home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Doc / ext.tex < prev    next >
LaTeX Document  |  1994-10-06  |  40.7 KB  |  [TEXT/R*ch]

open in: MacOS 8.1     |     Win98     |     DOS

view JSON data     |     view as text

This file was processed as: LaTeX Document (document/latex).

You can browse this item here: ext.tex

ConfidenceProgramDetectionMatch TypeSupport
100% dexvert LaTeX Document (document/latex) magic Supported
99% dexvert Texinfo Document (document/texInfo) magic Supported
1% dexvert Text File (text/txt) fallback Supported
100% file LaTeX document text default
99% file TeX document text default
98% file LaTeX document, ASCII text, with CR line terminators default
100% checkBytes Printable ASCII default
100% perlTextCheck Likely Text (Perl) default
100% siegfried fmt/281 LaTeX (Subdocument) default
100% detectItEasy Format: plain text[CR] default (weak)


id metadata
keyvalue
macFileType[TEXT]
macFileCreator[R*ch]



hex view
+--------+-------------------------+-------------------------+--------+--------+
|00000000| 5c 64 6f 63 75 6d 65 6e | 74 73 74 79 6c 65 5b 74 |\documen|tstyle[t|
|00000010| 77 6f 73 69 64 65 2c 31 | 31 70 74 2c 6d 79 66 6f |woside,1|1pt,myfo|
|00000020| 72 6d 61 74 5d 7b 72 65 | 70 6f 72 74 7d 0d 0d 5c |rmat]{re|port}..\|
|00000030| 74 69 74 6c 65 7b 45 78 | 74 65 6e 64 69 6e 67 20 |title{Ex|tending |
|00000040| 61 6e 64 20 45 6d 62 65 | 64 64 69 6e 67 20 74 68 |and Embe|dding th|
|00000050| 65 20 50 79 74 68 6f 6e | 20 49 6e 74 65 72 70 72 |e Python| Interpr|
|00000060| 65 74 65 72 7d 0d 0d 5c | 69 6e 70 75 74 7b 62 6f |eter}..\|input{bo|
|00000070| 69 6c 65 72 70 6c 61 74 | 65 7d 0d 0d 25 20 54 65 |ilerplat|e}..% Te|
|00000080| 6c 6c 20 5c 69 6e 64 65 | 78 20 74 6f 20 61 63 74 |ll \inde|x to act|
|00000090| 75 61 6c 6c 79 20 77 72 | 69 74 65 20 74 68 65 20 |ually wr|ite the |
|000000a0| 2e 69 64 78 20 66 69 6c | 65 0d 5c 6d 61 6b 65 69 |.idx fil|e.\makei|
|000000b0| 6e 64 65 78 0d 0d 5c 62 | 65 67 69 6e 7b 64 6f 63 |ndex..\b|egin{doc|
|000000c0| 75 6d 65 6e 74 7d 0d 0d | 5c 70 61 67 65 6e 75 6d |ument}..|\pagenum|
|000000d0| 62 65 72 69 6e 67 7b 72 | 6f 6d 61 6e 7d 0d 0d 5c |bering{r|oman}..\|
|000000e0| 6d 61 6b 65 74 69 74 6c | 65 0d 0d 5c 69 6e 70 75 |maketitl|e..\inpu|
|000000f0| 74 7b 63 6f 70 79 72 69 | 67 68 74 7d 0d 0d 5c 62 |t{copyri|ght}..\b|
|00000100| 65 67 69 6e 7b 61 62 73 | 74 72 61 63 74 7d 0d 0d |egin{abs|tract}..|
|00000110| 5c 6e 6f 69 6e 64 65 6e | 74 0d 54 68 69 73 20 64 |\noinden|t.This d|
|00000120| 6f 63 75 6d 65 6e 74 20 | 64 65 73 63 72 69 62 65 |ocument |describe|
|00000130| 73 20 68 6f 77 20 74 6f | 20 77 72 69 74 65 20 6d |s how to| write m|
|00000140| 6f 64 75 6c 65 73 20 69 | 6e 20 43 20 6f 72 20 5c |odules i|n C or \|
|00000150| 43 70 70 7b 7d 20 74 6f | 20 65 78 74 65 6e 64 20 |Cpp{} to| extend |
|00000160| 74 68 65 0d 50 79 74 68 | 6f 6e 20 69 6e 74 65 72 |the.Pyth|on inter|
|00000170| 70 72 65 74 65 72 2e 20 | 20 49 74 20 61 6c 73 6f |preter. | It also|
|00000180| 20 64 65 73 63 72 69 62 | 65 73 20 68 6f 77 20 74 | describ|es how t|
|00000190| 6f 20 75 73 65 20 50 79 | 74 68 6f 6e 20 61 73 20 |o use Py|thon as |
|000001a0| 61 6e 0d 60 65 6d 62 65 | 64 64 65 64 27 20 6c 61 |an.`embe|dded' la|
|000001b0| 6e 67 75 61 67 65 2c 20 | 61 6e 64 20 68 6f 77 20 |nguage, |and how |
|000001c0| 65 78 74 65 6e 73 69 6f | 6e 20 6d 6f 64 75 6c 65 |extensio|n module|
|000001d0| 73 20 63 61 6e 20 62 65 | 20 6c 6f 61 64 65 64 0d |s can be| loaded.|
|000001e0| 64 79 6e 61 6d 69 63 61 | 6c 6c 79 20 28 61 74 20 |dynamica|lly (at |
|000001f0| 72 75 6e 20 74 69 6d 65 | 29 20 69 6e 74 6f 20 74 |run time|) into t|
|00000200| 68 65 20 69 6e 74 65 72 | 70 72 65 74 65 72 2c 20 |he inter|preter, |
|00000210| 69 66 20 74 68 65 20 6f | 70 65 72 61 74 69 6e 67 |if the o|perating|
|00000220| 0d 73 79 73 74 65 6d 20 | 73 75 70 70 6f 72 74 73 |.system |supports|
|00000230| 20 74 68 69 73 20 66 65 | 61 74 75 72 65 2e 0d 0d | this fe|ature...|
|00000240| 5c 65 6e 64 7b 61 62 73 | 74 72 61 63 74 7d 0d 0d |\end{abs|tract}..|
|00000250| 5c 70 61 67 65 62 72 65 | 61 6b 0d 0d 7b 0d 5c 70 |\pagebre|ak..{.\p|
|00000260| 61 72 73 6b 69 70 20 3d | 20 30 6d 6d 0d 5c 74 61 |arskip =| 0mm.\ta|
|00000270| 62 6c 65 6f 66 63 6f 6e | 74 65 6e 74 73 0d 7d 0d |bleofcon|tents.}.|
|00000280| 0d 5c 70 61 67 65 62 72 | 65 61 6b 0d 0d 5c 70 61 |.\pagebr|eak..\pa|
|00000290| 67 65 6e 75 6d 62 65 72 | 69 6e 67 7b 61 72 61 62 |genumber|ing{arab|
|000002a0| 69 63 7d 0d 0d 0d 5c 63 | 68 61 70 74 65 72 7b 45 |ic}...\c|hapter{E|
|000002b0| 78 74 65 6e 64 69 6e 67 | 20 50 79 74 68 6f 6e 20 |xtending| Python |
|000002c0| 77 69 74 68 20 43 20 6f | 72 20 5c 43 70 70 7b 7d |with C o|r \Cpp{}|
|000002d0| 20 63 6f 64 65 7d 0d 0d | 0d 5c 73 65 63 74 69 6f | code}..|.\sectio|
|000002e0| 6e 7b 49 6e 74 72 6f 64 | 75 63 74 69 6f 6e 7d 0d |n{Introd|uction}.|
|000002f0| 0d 49 74 20 69 73 20 71 | 75 69 74 65 20 65 61 73 |.It is q|uite eas|
|00000300| 79 20 74 6f 20 61 64 64 | 20 6e 6f 6e 2d 73 74 61 |y to add| non-sta|
|00000310| 6e 64 61 72 64 20 62 75 | 69 6c 74 2d 69 6e 20 6d |ndard bu|ilt-in m|
|00000320| 6f 64 75 6c 65 73 20 74 | 6f 20 50 79 74 68 6f 6e |odules t|o Python|
|00000330| 2c 20 69 66 0d 79 6f 75 | 20 6b 6e 6f 77 20 68 6f |, if.you| know ho|
|00000340| 77 20 74 6f 20 70 72 6f | 67 72 61 6d 20 69 6e 20 |w to pro|gram in |
|00000350| 43 2e 20 20 41 20 62 75 | 69 6c 74 2d 69 6e 20 6d |C. A bu|ilt-in m|
|00000360| 6f 64 75 6c 65 20 6b 6e | 6f 77 6e 20 74 6f 20 74 |odule kn|own to t|
|00000370| 68 65 20 50 79 74 68 6f | 6e 0d 70 72 6f 67 72 61 |he Pytho|n.progra|
|00000380| 6d 6d 65 72 20 61 73 20 | 5c 63 6f 64 65 7b 66 6f |mmer as |\code{fo|
|00000390| 6f 7d 20 69 73 20 67 65 | 6e 65 72 61 6c 6c 79 20 |o} is ge|nerally |
|000003a0| 69 6d 70 6c 65 6d 65 6e | 74 65 64 20 62 79 20 61 |implemen|ted by a|
|000003b0| 20 66 69 6c 65 20 63 61 | 6c 6c 65 64 0d 5c 66 69 | file ca|lled.\fi|
|000003c0| 6c 65 7b 66 6f 6f 6d 6f | 64 75 6c 65 2e 63 7d 2e |le{foomo|dule.c}.|
|000003d0| 20 20 41 6c 6c 20 62 75 | 74 20 74 68 65 20 74 77 | All bu|t the tw|
|000003e0| 6f 20 6d 6f 73 74 20 65 | 73 73 65 6e 74 69 61 6c |o most e|ssential|
|000003f0| 20 73 74 61 6e 64 61 72 | 64 20 62 75 69 6c 74 2d | standar|d built-|
|00000400| 69 6e 0d 6d 6f 64 75 6c | 65 73 20 61 6c 73 6f 20 |in.modul|es also |
|00000410| 61 64 68 65 72 65 20 74 | 6f 20 74 68 69 73 20 63 |adhere t|o this c|
|00000420| 6f 6e 76 65 6e 74 69 6f | 6e 2c 20 61 6e 64 20 69 |onventio|n, and i|
|00000430| 6e 20 66 61 63 74 20 73 | 6f 6d 65 20 6f 66 20 74 |n fact s|ome of t|
|00000440| 68 65 6d 20 66 6f 72 6d | 0d 65 78 63 65 6c 6c 65 |hem form|.excelle|
|00000450| 6e 74 20 65 78 61 6d 70 | 6c 65 73 20 6f 66 20 68 |nt examp|les of h|
|00000460| 6f 77 20 74 6f 20 63 72 | 65 61 74 65 20 61 6e 20 |ow to cr|eate an |
|00000470| 65 78 74 65 6e 73 69 6f | 6e 2e 0d 0d 45 78 74 65 |extensio|n...Exte|
|00000480| 6e 73 69 6f 6e 20 6d 6f | 64 75 6c 65 73 20 63 61 |nsion mo|dules ca|
|00000490| 6e 20 64 6f 20 74 77 6f | 20 74 68 69 6e 67 73 20 |n do two| things |
|000004a0| 74 68 61 74 20 63 61 6e | 27 74 20 62 65 20 64 6f |that can|'t be do|
|000004b0| 6e 65 20 64 69 72 65 63 | 74 6c 79 20 69 6e 0d 50 |ne direc|tly in.P|
|000004c0| 79 74 68 6f 6e 3a 20 74 | 68 65 79 20 63 61 6e 20 |ython: t|hey can |
|000004d0| 69 6d 70 6c 65 6d 65 6e | 74 20 6e 65 77 20 64 61 |implemen|t new da|
|000004e0| 74 61 20 74 79 70 65 73 | 20 28 77 68 69 63 68 20 |ta types| (which |
|000004f0| 61 72 65 20 64 69 66 66 | 65 72 65 6e 74 20 66 72 |are diff|erent fr|
|00000500| 6f 6d 0d 63 6c 61 73 73 | 65 73 2c 20 62 79 20 74 |om.class|es, by t|
|00000510| 68 65 20 77 61 79 29 2c | 20 61 6e 64 20 74 68 65 |he way),| and the|
|00000520| 79 20 63 61 6e 20 6d 61 | 6b 65 20 73 79 73 74 65 |y can ma|ke syste|
|00000530| 6d 20 63 61 6c 6c 73 20 | 6f 72 20 63 61 6c 6c 20 |m calls |or call |
|00000540| 43 20 6c 69 62 72 61 72 | 79 0d 66 75 6e 63 74 69 |C librar|y.functi|
|00000550| 6f 6e 73 2e 20 20 20 57 | 65 27 6c 6c 20 73 65 65 |ons. W|e'll see|
|00000560| 20 68 6f 77 20 62 6f 74 | 68 20 74 79 70 65 73 20 | how bot|h types |
|00000570| 6f 66 20 65 78 74 65 6e | 73 69 6f 6e 20 61 72 65 |of exten|sion are|
|00000580| 20 69 6d 70 6c 65 6d 65 | 6e 74 65 64 20 62 79 0d | impleme|nted by.|
|00000590| 65 78 61 6d 69 6e 69 6e | 67 20 74 68 65 20 63 6f |examinin|g the co|
|000005a0| 64 65 20 66 6f 72 20 61 | 20 50 79 74 68 6f 6e 20 |de for a| Python |
|000005b0| 63 75 72 73 65 73 20 69 | 6e 74 65 72 66 61 63 65 |curses i|nterface|
|000005c0| 2e 0d 0d 4e 6f 74 65 3a | 20 75 6e 6c 65 73 73 20 |...Note:| unless |
|000005d0| 6f 74 68 65 72 77 69 73 | 65 20 6d 65 6e 74 69 6f |otherwis|e mentio|
|000005e0| 6e 65 64 2c 20 61 6c 6c | 20 66 69 6c 65 20 72 65 |ned, all| file re|
|000005f0| 66 65 72 65 6e 63 65 73 | 20 69 6e 20 74 68 69 73 |ferences| in this|
|00000600| 0d 64 6f 63 75 6d 65 6e | 74 20 61 72 65 20 72 65 |.documen|t are re|
|00000610| 6c 61 74 69 76 65 20 74 | 6f 20 74 68 65 20 74 6f |lative t|o the to|
|00000620| 70 6c 65 76 65 6c 20 64 | 69 72 65 63 74 6f 72 79 |plevel d|irectory|
|00000630| 20 6f 66 20 74 68 65 20 | 50 79 74 68 6f 6e 0d 64 | of the |Python.d|
|00000640| 69 73 74 72 69 62 75 74 | 69 6f 6e 20 2d 2d 2d 20 |istribut|ion --- |
|00000650| 69 2e 65 2e 20 74 68 65 | 20 64 69 72 65 63 74 6f |i.e. the| directo|
|00000660| 72 79 20 74 68 61 74 20 | 63 6f 6e 74 61 69 6e 73 |ry that |contains|
|00000670| 20 74 68 65 20 5c 66 69 | 6c 65 7b 63 6f 6e 66 69 | the \fi|le{confi|
|00000680| 67 75 72 65 7d 0d 73 63 | 72 69 70 74 2e 0d 0d 54 |gure}.sc|ript...T|
|00000690| 68 65 20 63 6f 6d 70 69 | 6c 61 74 69 6f 6e 20 6f |he compi|lation o|
|000006a0| 66 20 61 6e 20 65 78 74 | 65 6e 73 69 6f 6e 20 6d |f an ext|ension m|
|000006b0| 6f 64 75 6c 65 20 64 65 | 70 65 6e 64 73 20 6f 6e |odule de|pends on|
|000006c0| 20 79 6f 75 72 20 73 79 | 73 74 65 6d 20 73 65 74 | your sy|stem set|
|000006d0| 75 70 0d 61 6e 64 20 74 | 68 65 20 69 6e 74 65 6e |up.and t|he inten|
|000006e0| 64 65 64 20 75 73 65 20 | 6f 66 20 74 68 65 20 6d |ded use |of the m|
|000006f0| 6f 64 75 6c 65 3b 20 64 | 65 74 61 69 6c 73 20 61 |odule; d|etails a|
|00000700| 72 65 20 67 69 76 65 6e | 20 69 6e 20 61 20 6c 61 |re given| in a la|
|00000710| 74 65 72 0d 73 65 63 74 | 69 6f 6e 2e 0d 0d 0d 5c |ter.sect|ion....\|
|00000720| 73 65 63 74 69 6f 6e 7b | 41 20 66 69 72 73 74 20 |section{|A first |
|00000730| 6c 6f 6f 6b 20 61 74 20 | 74 68 65 20 63 6f 64 65 |look at |the code|
|00000740| 7d 0d 0d 49 74 20 69 73 | 20 69 6d 70 6f 72 74 61 |}..It is| importa|
|00000750| 6e 74 20 6e 6f 74 20 74 | 6f 20 62 65 20 69 6d 70 |nt not t|o be imp|
|00000760| 72 65 73 73 65 64 20 62 | 79 20 74 68 65 20 73 69 |ressed b|y the si|
|00000770| 7a 65 20 61 6e 64 20 63 | 6f 6d 70 6c 65 78 69 74 |ze and c|omplexit|
|00000780| 79 20 6f 66 0d 74 68 65 | 20 61 76 65 72 61 67 65 |y of.the| average|
|00000790| 20 65 78 74 65 6e 73 69 | 6f 6e 20 6d 6f 64 75 6c | extensi|on modul|
|000007a0| 65 3b 20 6d 75 63 68 20 | 6f 66 20 74 68 69 73 20 |e; much |of this |
|000007b0| 69 73 20 73 74 72 61 69 | 67 68 74 66 6f 72 77 61 |is strai|ghtforwa|
|000007c0| 72 64 0d 60 62 6f 69 6c | 65 72 70 6c 61 74 65 27 |rd.`boil|erplate'|
|000007d0| 20 63 6f 64 65 20 28 73 | 74 61 72 74 69 6e 67 20 | code (s|tarting |
|000007e0| 72 69 67 68 74 20 77 69 | 74 68 20 74 68 65 20 63 |right wi|th the c|
|000007f0| 6f 70 79 72 69 67 68 74 | 20 6e 6f 74 69 63 65 29 |opyright| notice)|
|00000800| 21 0d 0d 4c 65 74 27 73 | 20 73 6b 69 70 20 74 68 |!..Let's| skip th|
|00000810| 65 20 62 6f 69 6c 65 72 | 70 6c 61 74 65 20 61 6e |e boiler|plate an|
|00000820| 64 20 68 61 76 65 20 61 | 20 6c 6f 6f 6b 20 61 74 |d have a| look at|
|00000830| 20 61 6e 20 69 6e 74 65 | 72 65 73 74 69 6e 67 20 | an inte|resting |
|00000840| 66 75 6e 63 74 69 6f 6e | 0d 69 6e 20 5c 66 69 6c |function|.in \fil|
|00000850| 65 7b 70 6f 73 69 78 6d | 6f 64 75 6c 65 2e 63 7d |e{posixm|odule.c}|
|00000860| 20 66 69 72 73 74 3a 0d | 0d 5c 62 65 67 69 6e 7b | first:.|.\begin{|
|00000870| 76 65 72 62 61 74 69 6d | 7d 0d 20 20 20 20 73 74 |verbatim|}. st|
|00000880| 61 74 69 63 20 6f 62 6a | 65 63 74 20 2a 0d 20 20 |atic obj|ect *. |
|00000890| 20 20 70 6f 73 69 78 5f | 73 79 73 74 65 6d 28 73 | posix_|system(s|
|000008a0| 65 6c 66 2c 20 61 72 67 | 73 29 0d 20 20 20 20 20 |elf, arg|s). |
|000008b0| 20 20 20 6f 62 6a 65 63 | 74 20 2a 73 65 6c 66 3b | objec|t *self;|
|000008c0| 0d 20 20 20 20 20 20 20 | 20 6f 62 6a 65 63 74 20 |. | object |
|000008d0| 2a 61 72 67 73 3b 0d 20 | 20 20 20 7b 0d 20 20 20 |*args;. | {. |
|000008e0| 20 20 20 20 20 63 68 61 | 72 20 2a 63 6f 6d 6d 61 | cha|r *comma|
|000008f0| 6e 64 3b 0d 20 20 20 20 | 20 20 20 20 69 6e 74 20 |nd;. | int |
|00000900| 73 74 73 3b 0d 20 20 20 | 20 20 20 20 20 69 66 20 |sts;. | if |
|00000910| 28 21 67 65 74 61 72 67 | 73 28 61 72 67 73 2c 20 |(!getarg|s(args, |
|00000920| 22 73 22 2c 20 26 63 6f | 6d 6d 61 6e 64 29 29 0d |"s", &co|mmand)).|
|00000930| 20 20 20 20 20 20 20 20 | 20 20 20 20 72 65 74 75 | | retu|
|00000940| 72 6e 20 4e 55 4c 4c 3b | 0d 20 20 20 20 20 20 20 |rn NULL;|. |
|00000950| 20 73 74 73 20 3d 20 73 | 79 73 74 65 6d 28 63 6f | sts = s|ystem(co|
|00000960| 6d 6d 61 6e 64 29 3b 0d | 20 20 20 20 20 20 20 20 |mmand);.| |
|00000970| 72 65 74 75 72 6e 20 6d | 6b 76 61 6c 75 65 28 22 |return m|kvalue("|
|00000980| 69 22 2c 20 73 74 73 29 | 3b 0d 20 20 20 20 7d 0d |i", sts)|;. }.|
|00000990| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 0d 0d |\end{ver|batim}..|
|000009a0| 54 68 69 73 20 69 73 20 | 74 68 65 20 70 72 6f 74 |This is |the prot|
|000009b0| 6f 74 79 70 69 63 61 6c | 20 74 6f 70 2d 6c 65 76 |otypical| top-lev|
|000009c0| 65 6c 20 66 75 6e 63 74 | 69 6f 6e 20 69 6e 20 61 |el funct|ion in a|
|000009d0| 6e 20 65 78 74 65 6e 73 | 69 6f 6e 20 6d 6f 64 75 |n extens|ion modu|
|000009e0| 6c 65 2e 0d 49 74 20 77 | 69 6c 6c 20 62 65 20 63 |le..It w|ill be c|
|000009f0| 61 6c 6c 65 64 20 28 77 | 65 27 6c 6c 20 73 65 65 |alled (w|e'll see|
|00000a00| 20 6c 61 74 65 72 20 68 | 6f 77 29 20 77 68 65 6e | later h|ow) when|
|00000a10| 20 74 68 65 20 50 79 74 | 68 6f 6e 20 70 72 6f 67 | the Pyt|hon prog|
|00000a20| 72 61 6d 0d 65 78 65 63 | 75 74 65 73 20 73 74 61 |ram.exec|utes sta|
|00000a30| 74 65 6d 65 6e 74 73 20 | 6c 69 6b 65 0d 0d 5c 62 |tements |like..\b|
|00000a40| 65 67 69 6e 7b 76 65 72 | 62 61 74 69 6d 7d 0d 20 |egin{ver|batim}. |
|00000a50| 20 20 20 3e 3e 3e 20 69 | 6d 70 6f 72 74 20 70 6f | >>> i|mport po|
|00000a60| 73 69 78 0d 20 20 20 20 | 3e 3e 3e 20 73 74 73 20 |six. |>>> sts |
|00000a70| 3d 20 70 6f 73 69 78 2e | 73 79 73 74 65 6d 28 27 |= posix.|system('|
|00000a80| 6c 73 20 2d 6c 27 29 0d | 5c 65 6e 64 7b 76 65 72 |ls -l').|\end{ver|
|00000a90| 62 61 74 69 6d 7d 0d 0d | 54 68 65 72 65 20 69 73 |batim}..|There is|
|00000aa0| 20 61 20 73 74 72 61 69 | 67 68 74 66 6f 72 77 61 | a strai|ghtforwa|
|00000ab0| 72 64 20 74 72 61 6e 73 | 6c 61 74 69 6f 6e 20 66 |rd trans|lation f|
|00000ac0| 72 6f 6d 20 74 68 65 20 | 61 72 67 75 6d 65 6e 74 |rom the |argument|
|00000ad0| 73 20 74 6f 20 74 68 65 | 20 63 61 6c 6c 0d 69 6e |s to the| call.in|
|00000ae0| 20 50 79 74 68 6f 6e 20 | 28 68 65 72 65 20 74 68 | Python |(here th|
|00000af0| 65 20 73 69 6e 67 6c 65 | 20 65 78 70 72 65 73 73 |e single| express|
|00000b00| 69 6f 6e 20 5c 63 6f 64 | 65 7b 27 6c 73 20 2d 6c |ion \cod|e{'ls -l|
|00000b10| 27 7d 29 20 74 6f 20 74 | 68 65 20 61 72 67 75 6d |'}) to t|he argum|
|00000b20| 65 6e 74 73 20 74 68 61 | 74 0d 61 72 65 20 70 61 |ents tha|t.are pa|
|00000b30| 73 73 65 64 20 74 6f 20 | 74 68 65 20 43 20 66 75 |ssed to |the C fu|
|00000b40| 6e 63 74 69 6f 6e 2e 20 | 20 54 68 65 20 43 20 66 |nction. | The C f|
|00000b50| 75 6e 63 74 69 6f 6e 20 | 61 6c 77 61 79 73 20 68 |unction |always h|
|00000b60| 61 73 20 74 77 6f 0d 70 | 61 72 61 6d 65 74 65 72 |as two.p|arameter|
|00000b70| 73 2c 20 63 6f 6e 76 65 | 6e 74 69 6f 6e 61 6c 6c |s, conve|ntionall|
|00000b80| 79 20 6e 61 6d 65 64 20 | 5c 76 61 72 7b 73 65 6c |y named |\var{sel|
|00000b90| 66 7d 20 61 6e 64 20 5c | 76 61 72 7b 61 72 67 73 |f} and \|var{args|
|00000ba0| 7d 2e 20 20 54 68 65 0d | 5c 76 61 72 7b 73 65 6c |}. The.|\var{sel|
|00000bb0| 66 7d 20 61 72 67 75 6d | 65 6e 74 20 69 73 20 75 |f} argum|ent is u|
|00000bc0| 73 65 64 20 77 68 65 6e | 20 74 68 65 20 43 20 66 |sed when| the C f|
|00000bd0| 75 6e 63 74 69 6f 6e 20 | 69 6d 70 6c 65 6d 65 6e |unction |implemen|
|00000be0| 74 73 20 61 20 62 75 69 | 6c 74 69 6e 0d 6d 65 74 |ts a bui|ltin.met|
|00000bf0| 68 6f 64 2d 2d 2d 74 68 | 69 73 20 77 69 6c 6c 20 |hod---th|is will |
|00000c00| 62 65 20 64 69 73 63 75 | 73 73 65 64 20 6c 61 74 |be discu|ssed lat|
|00000c10| 65 72 2e 0d 49 6e 20 74 | 68 65 20 65 78 61 6d 70 |er..In t|he examp|
|00000c20| 6c 65 2c 20 5c 76 61 72 | 7b 73 65 6c 66 7d 20 77 |le, \var|{self} w|
|00000c30| 69 6c 6c 20 61 6c 77 61 | 79 73 20 62 65 20 61 20 |ill alwa|ys be a |
|00000c40| 5c 63 6f 64 65 7b 4e 55 | 4c 4c 7d 20 70 6f 69 6e |\code{NU|LL} poin|
|00000c50| 74 65 72 2c 20 73 69 6e | 63 65 0d 77 65 20 61 72 |ter, sin|ce.we ar|
|00000c60| 65 20 64 65 66 69 6e 69 | 6e 67 20 61 20 66 75 6e |e defini|ng a fun|
|00000c70| 63 74 69 6f 6e 2c 20 6e | 6f 74 20 61 20 6d 65 74 |ction, n|ot a met|
|00000c80| 68 6f 64 20 28 74 68 69 | 73 20 69 73 20 64 6f 6e |hod (thi|s is don|
|00000c90| 65 20 73 6f 20 74 68 61 | 74 20 74 68 65 0d 69 6e |e so tha|t the.in|
|00000ca0| 74 65 72 70 72 65 74 65 | 72 20 64 6f 65 73 6e 27 |terprete|r doesn'|
|00000cb0| 74 20 68 61 76 65 20 74 | 6f 20 75 6e 64 65 72 73 |t have t|o unders|
|00000cc0| 74 61 6e 64 20 74 77 6f | 20 64 69 66 66 65 72 65 |tand two| differe|
|00000cd0| 6e 74 20 74 79 70 65 73 | 20 6f 66 20 43 0d 66 75 |nt types| of C.fu|
|00000ce0| 6e 63 74 69 6f 6e 73 29 | 2e 0d 0d 54 68 65 20 5c |nctions)|...The \|
|00000cf0| 76 61 72 7b 61 72 67 73 | 7d 20 70 61 72 61 6d 65 |var{args|} parame|
|00000d00| 74 65 72 20 77 69 6c 6c | 20 62 65 20 61 20 70 6f |ter will| be a po|
|00000d10| 69 6e 74 65 72 20 74 6f | 20 61 20 50 79 74 68 6f |inter to| a Pytho|
|00000d20| 6e 20 6f 62 6a 65 63 74 | 2c 20 6f 72 0d 5c 63 6f |n object|, or.\co|
|00000d30| 64 65 7b 4e 55 4c 4c 7d | 20 69 66 20 74 68 65 20 |de{NULL}| if the |
|00000d40| 50 79 74 68 6f 6e 20 66 | 75 6e 63 74 69 6f 6e 2f |Python f|unction/|
|00000d50| 6d 65 74 68 6f 64 20 77 | 61 73 20 63 61 6c 6c 65 |method w|as calle|
|00000d60| 64 20 77 69 74 68 6f 75 | 74 0d 61 72 67 75 6d 65 |d withou|t.argume|
|00000d70| 6e 74 73 2e 20 20 49 74 | 20 69 73 20 6e 65 63 65 |nts. It| is nece|
|00000d80| 73 73 61 72 79 20 74 6f | 20 64 6f 20 66 75 6c 6c |ssary to| do full|
|00000d90| 20 61 72 67 75 6d 65 6e | 74 20 74 79 70 65 20 63 | argumen|t type c|
|00000da0| 68 65 63 6b 69 6e 67 20 | 6f 6e 20 65 61 63 68 0d |hecking |on each.|
|00000db0| 63 61 6c 6c 2c 20 73 69 | 6e 63 65 20 6f 74 68 65 |call, si|nce othe|
|00000dc0| 72 77 69 73 65 20 74 68 | 65 20 50 79 74 68 6f 6e |rwise th|e Python|
|00000dd0| 20 75 73 65 72 20 77 6f | 75 6c 64 20 62 65 20 61 | user wo|uld be a|
|00000de0| 62 6c 65 20 74 6f 20 63 | 61 75 73 65 20 74 68 65 |ble to c|ause the|
|00000df0| 0d 50 79 74 68 6f 6e 20 | 69 6e 74 65 72 70 72 65 |.Python |interpre|
|00000e00| 74 65 72 20 74 6f 20 60 | 64 75 6d 70 20 63 6f 72 |ter to `|dump cor|
|00000e10| 65 27 20 62 79 20 70 61 | 73 73 69 6e 67 20 69 6e |e' by pa|ssing in|
|00000e20| 76 61 6c 69 64 20 61 72 | 67 75 6d 65 6e 74 73 20 |valid ar|guments |
|00000e30| 74 6f 20 61 0d 66 75 6e | 63 74 69 6f 6e 20 69 6e |to a.fun|ction in|
|00000e40| 20 61 6e 20 65 78 74 65 | 6e 73 69 6f 6e 20 6d 6f | an exte|nsion mo|
|00000e50| 64 75 6c 65 2e 20 20 42 | 65 63 61 75 73 65 20 61 |dule. B|ecause a|
|00000e60| 72 67 75 6d 65 6e 74 20 | 63 68 65 63 6b 69 6e 67 |rgument |checking|
|00000e70| 20 61 6e 64 0d 63 6f 6e | 76 65 72 74 69 6e 67 20 | and.con|verting |
|00000e80| 61 72 67 75 6d 65 6e 74 | 73 20 74 6f 20 43 20 61 |argument|s to C a|
|00000e90| 72 65 20 73 75 63 68 20 | 63 6f 6d 6d 6f 6e 20 74 |re such |common t|
|00000ea0| 61 73 6b 73 2c 20 74 68 | 65 72 65 27 73 20 61 20 |asks, th|ere's a |
|00000eb0| 67 65 6e 65 72 61 6c 0d | 66 75 6e 63 74 69 6f 6e |general.|function|
|00000ec0| 20 69 6e 20 74 68 65 20 | 50 79 74 68 6f 6e 20 69 | in the |Python i|
|00000ed0| 6e 74 65 72 70 72 65 74 | 65 72 20 74 68 61 74 20 |nterpret|er that |
|00000ee0| 63 6f 6d 62 69 6e 65 73 | 20 74 68 65 6d 3a 0d 5c |combines| them:.\|
|00000ef0| 63 6f 64 65 7b 67 65 74 | 61 72 67 73 28 29 7d 2e |code{get|args()}.|
|00000f00| 20 20 49 74 20 75 73 65 | 73 20 61 20 74 65 6d 70 | It use|s a temp|
|00000f10| 6c 61 74 65 20 73 74 72 | 69 6e 67 20 74 6f 20 64 |late str|ing to d|
|00000f20| 65 74 65 72 6d 69 6e 65 | 20 62 6f 74 68 20 74 68 |etermine| both th|
|00000f30| 65 0d 74 79 70 65 73 20 | 6f 66 20 74 68 65 20 50 |e.types |of the P|
|00000f40| 79 74 68 6f 6e 20 61 72 | 67 75 6d 65 6e 74 20 61 |ython ar|gument a|
|00000f50| 6e 64 20 74 68 65 20 74 | 79 70 65 73 20 6f 66 20 |nd the t|ypes of |
|00000f60| 74 68 65 20 43 20 76 61 | 72 69 61 62 6c 65 73 20 |the C va|riables |
|00000f70| 69 6e 74 6f 0d 77 68 69 | 63 68 20 69 74 20 73 68 |into.whi|ch it sh|
|00000f80| 6f 75 6c 64 20 73 74 6f | 72 65 20 74 68 65 20 63 |ould sto|re the c|
|00000f90| 6f 6e 76 65 72 74 65 64 | 20 76 61 6c 75 65 73 2e |onverted| values.|
|00000fa0| 5c 66 6f 6f 74 6e 6f 74 | 65 7b 54 68 65 72 65 20 |\footnot|e{There |
|00000fb0| 61 72 65 0d 63 6f 6e 76 | 65 6e 69 65 6e 63 65 20 |are.conv|enience |
|00000fc0| 6d 61 63 72 6f 73 20 5c | 63 6f 64 65 7b 67 65 74 |macros \|code{get|
|00000fd0| 6e 6f 61 72 67 28 29 7d | 2c 20 5c 63 6f 64 65 7b |noarg()}|, \code{|
|00000fe0| 67 65 74 73 74 72 61 72 | 67 28 29 7d 2c 0d 5c 63 |getstrar|g()},.\c|
|00000ff0| 6f 64 65 7b 67 65 74 69 | 6e 74 61 72 67 28 29 7d |ode{geti|ntarg()}|
|00001000| 2c 20 65 74 63 2e 2c 20 | 66 6f 72 20 6d 61 6e 79 |, etc., |for many|
|00001010| 20 63 6f 6d 6d 6f 6e 20 | 66 6f 72 6d 73 20 6f 66 | common |forms of|
|00001020| 20 5c 63 6f 64 65 7b 67 | 65 74 61 72 67 73 28 29 | \code{g|etargs()|
|00001030| 7d 0d 74 65 6d 70 6c 61 | 74 65 73 2e 20 20 54 68 |}.templa|tes. Th|
|00001040| 65 73 65 20 61 72 65 20 | 72 65 6c 69 63 73 20 66 |ese are |relics f|
|00001050| 72 6f 6d 20 74 68 65 20 | 70 61 73 74 3b 20 74 68 |rom the |past; th|
|00001060| 65 20 72 65 63 6f 6d 6d | 65 6e 64 65 64 20 70 72 |e recomm|ended pr|
|00001070| 61 63 74 69 63 65 0d 69 | 73 20 74 6f 20 63 61 6c |actice.i|s to cal|
|00001080| 6c 20 5c 63 6f 64 65 7b | 67 65 74 61 72 67 73 28 |l \code{|getargs(|
|00001090| 29 7d 20 64 69 72 65 63 | 74 6c 79 2e 7d 20 20 28 |)} direc|tly.} (|
|000010a0| 4d 6f 72 65 20 61 62 6f | 75 74 20 74 68 69 73 20 |More abo|ut this |
|000010b0| 6c 61 74 65 72 2e 29 0d | 0d 49 66 20 5c 63 6f 64 |later.).|.If \cod|
|000010c0| 65 7b 67 65 74 61 72 67 | 73 28 29 7d 20 72 65 74 |e{getarg|s()} ret|
|000010d0| 75 72 6e 73 20 6e 6f 6e | 7a 65 72 6f 2c 20 74 68 |urns non|zero, th|
|000010e0| 65 20 61 72 67 75 6d 65 | 6e 74 20 6c 69 73 74 20 |e argume|nt list |
|000010f0| 68 61 73 20 74 68 65 20 | 72 69 67 68 74 0d 74 79 |has the |right.ty|
|00001100| 70 65 20 61 6e 64 20 69 | 74 73 20 63 6f 6d 70 6f |pe and i|ts compo|
|00001110| 6e 65 6e 74 73 20 68 61 | 76 65 20 62 65 65 6e 20 |nents ha|ve been |
|00001120| 73 74 6f 72 65 64 20 69 | 6e 20 74 68 65 20 76 61 |stored i|n the va|
|00001130| 72 69 61 62 6c 65 73 20 | 77 68 6f 73 65 0d 61 64 |riables |whose.ad|
|00001140| 64 72 65 73 73 65 73 20 | 61 72 65 20 70 61 73 73 |dresses |are pass|
|00001150| 65 64 2e 20 20 49 66 20 | 69 74 20 72 65 74 75 72 |ed. If |it retur|
|00001160| 6e 73 20 7a 65 72 6f 2c | 20 61 6e 20 65 72 72 6f |ns zero,| an erro|
|00001170| 72 20 68 61 73 20 6f 63 | 63 75 72 72 65 64 2e 20 |r has oc|curred. |
|00001180| 20 49 6e 0d 74 68 65 20 | 6c 61 74 74 65 72 20 63 | In.the |latter c|
|00001190| 61 73 65 20 69 74 20 68 | 61 73 20 61 6c 72 65 61 |ase it h|as alrea|
|000011a0| 64 79 20 72 61 69 73 65 | 64 20 61 6e 20 61 70 70 |dy raise|d an app|
|000011b0| 72 6f 70 72 69 61 74 65 | 20 65 78 63 65 70 74 69 |ropriate| excepti|
|000011c0| 6f 6e 20 62 79 20 73 6f | 0d 74 68 65 20 63 61 6c |on by so|.the cal|
|000011d0| 6c 69 6e 67 20 66 75 6e | 63 74 69 6f 6e 20 73 68 |ling fun|ction sh|
|000011e0| 6f 75 6c 64 20 72 65 74 | 75 72 6e 20 5c 63 6f 64 |ould ret|urn \cod|
|000011f0| 65 7b 4e 55 4c 4c 7d 20 | 69 6d 6d 65 64 69 61 74 |e{NULL} |immediat|
|00001200| 65 6c 79 20 2d 2d 2d 20 | 73 65 65 20 74 68 65 0d |ely --- |see the.|
|00001210| 6e 65 78 74 20 73 65 63 | 74 69 6f 6e 2e 0d 0d 0d |next sec|tion....|
|00001220| 5c 73 65 63 74 69 6f 6e | 7b 49 6e 74 65 72 6d 65 |\section|{Interme|
|00001230| 7a 7a 6f 3a 20 65 72 72 | 6f 72 73 20 61 6e 64 20 |zzo: err|ors and |
|00001240| 65 78 63 65 70 74 69 6f | 6e 73 7d 0d 0d 41 6e 20 |exceptio|ns}..An |
|00001250| 69 6d 70 6f 72 74 61 6e | 74 20 63 6f 6e 76 65 6e |importan|t conven|
|00001260| 74 69 6f 6e 20 74 68 72 | 6f 75 67 68 6f 75 74 20 |tion thr|oughout |
|00001270| 74 68 65 20 50 79 74 68 | 6f 6e 20 69 6e 74 65 72 |the Pyth|on inter|
|00001280| 70 72 65 74 65 72 20 69 | 73 20 74 68 65 0d 66 6f |preter i|s the.fo|
|00001290| 6c 6c 6f 77 69 6e 67 3a | 20 77 68 65 6e 20 61 20 |llowing:| when a |
|000012a0| 66 75 6e 63 74 69 6f 6e | 20 66 61 69 6c 73 2c 20 |function| fails, |
|000012b0| 69 74 20 73 68 6f 75 6c | 64 20 73 65 74 20 61 6e |it shoul|d set an|
|000012c0| 20 65 78 63 65 70 74 69 | 6f 6e 20 63 6f 6e 64 69 | excepti|on condi|
|000012d0| 74 69 6f 6e 0d 61 6e 64 | 20 72 65 74 75 72 6e 20 |tion.and| return |
|000012e0| 61 6e 20 65 72 72 6f 72 | 20 76 61 6c 75 65 20 28 |an error| value (|
|000012f0| 6f 66 74 65 6e 20 61 20 | 5c 63 6f 64 65 7b 4e 55 |often a |\code{NU|
|00001300| 4c 4c 7d 20 70 6f 69 6e | 74 65 72 29 2e 20 20 45 |LL} poin|ter). E|
|00001310| 78 63 65 70 74 69 6f 6e | 73 0d 61 72 65 20 73 74 |xception|s.are st|
|00001320| 6f 72 65 64 20 69 6e 20 | 61 20 73 74 61 74 69 63 |ored in |a static|
|00001330| 20 67 6c 6f 62 61 6c 20 | 76 61 72 69 61 62 6c 65 | global |variable|
|00001340| 20 69 6e 20 5c 66 69 6c | 65 7b 50 79 74 68 6f 6e | in \fil|e{Python|
|00001350| 2f 65 72 72 6f 72 73 2e | 63 7d 3b 20 69 66 0d 74 |/errors.|c}; if.t|
|00001360| 68 69 73 20 76 61 72 69 | 61 62 6c 65 20 69 73 20 |his vari|able is |
|00001370| 5c 63 6f 64 65 7b 4e 55 | 4c 4c 7d 20 6e 6f 20 65 |\code{NU|LL} no e|
|00001380| 78 63 65 70 74 69 6f 6e | 20 68 61 73 20 6f 63 63 |xception| has occ|
|00001390| 75 72 72 65 64 2e 20 20 | 41 20 73 65 63 6f 6e 64 |urred. |A second|
|000013a0| 0d 73 74 61 74 69 63 20 | 67 6c 6f 62 61 6c 20 76 |.static |global v|
|000013b0| 61 72 69 61 62 6c 65 20 | 73 74 6f 72 65 73 20 74 |ariable |stores t|
|000013c0| 68 65 20 60 61 73 73 6f | 63 69 61 74 65 64 20 76 |he `asso|ciated v|
|000013d0| 61 6c 75 65 27 20 6f 66 | 20 74 68 65 20 65 78 63 |alue' of| the exc|
|000013e0| 65 70 74 69 6f 6e 0d 2d | 2d 2d 20 74 68 65 20 73 |eption.-|-- the s|
|000013f0| 65 63 6f 6e 64 20 61 72 | 67 75 6d 65 6e 74 20 74 |econd ar|gument t|
|00001400| 6f 20 5c 63 6f 64 65 7b | 72 61 69 73 65 7d 2e 0d |o \code{|raise}..|
|00001410| 0d 54 68 65 20 66 69 6c | 65 20 5c 66 69 6c 65 7b |.The fil|e \file{|
|00001420| 65 72 72 6f 72 73 2e 68 | 7d 20 64 65 63 6c 61 72 |errors.h|} declar|
|00001430| 65 73 20 61 20 68 6f 73 | 74 20 6f 66 20 66 75 6e |es a hos|t of fun|
|00001440| 63 74 69 6f 6e 73 20 74 | 6f 20 73 65 74 20 76 61 |ctions t|o set va|
|00001450| 72 69 6f 75 73 0d 74 79 | 70 65 73 20 6f 66 20 65 |rious.ty|pes of e|
|00001460| 78 63 65 70 74 69 6f 6e | 73 2e 20 20 54 68 65 20 |xception|s. The |
|00001470| 6d 6f 73 74 20 63 6f 6d | 6d 6f 6e 20 6f 6e 65 20 |most com|mon one |
|00001480| 69 73 20 5c 63 6f 64 65 | 7b 65 72 72 5f 73 65 74 |is \code|{err_set|
|00001490| 73 74 72 28 29 7d 20 2d | 2d 2d 0d 69 74 73 20 61 |str()} -|--.its a|
|000014a0| 72 67 75 6d 65 6e 74 73 | 20 61 72 65 20 61 6e 20 |rguments| are an |
|000014b0| 65 78 63 65 70 74 69 6f | 6e 20 6f 62 6a 65 63 74 |exceptio|n object|
|000014c0| 20 28 65 2e 67 2e 20 5c | 63 6f 64 65 7b 52 75 6e | (e.g. \|code{Run|
|000014d0| 74 69 6d 65 45 72 72 6f | 72 7d 20 2d 2d 2d 0d 61 |timeErro|r} ---.a|
|000014e0| 63 74 75 61 6c 6c 79 20 | 69 74 20 63 61 6e 20 62 |ctually |it can b|
|000014f0| 65 20 61 6e 79 20 73 74 | 72 69 6e 67 20 6f 62 6a |e any st|ring obj|
|00001500| 65 63 74 29 20 61 6e 64 | 20 61 20 43 20 73 74 72 |ect) and| a C str|
|00001510| 69 6e 67 20 69 6e 64 69 | 63 61 74 69 6e 67 20 74 |ing indi|cating t|
|00001520| 68 65 0d 63 61 75 73 65 | 20 6f 66 20 74 68 65 20 |he.cause| of the |
|00001530| 65 72 72 6f 72 20 28 74 | 68 69 73 20 69 73 20 63 |error (t|his is c|
|00001540| 6f 6e 76 65 72 74 65 64 | 20 74 6f 20 61 20 73 74 |onverted| to a st|
|00001550| 72 69 6e 67 20 6f 62 6a | 65 63 74 20 61 6e 64 20 |ring obj|ect and |
|00001560| 73 74 6f 72 65 64 20 61 | 73 0d 74 68 65 20 60 61 |stored a|s.the `a|
|00001570| 73 73 6f 63 69 61 74 65 | 64 20 76 61 6c 75 65 27 |ssociate|d value'|
|00001580| 20 6f 66 20 74 68 65 20 | 65 78 63 65 70 74 69 6f | of the |exceptio|
|00001590| 6e 29 2e 20 20 41 6e 6f | 74 68 65 72 20 75 73 65 |n). Ano|ther use|
|000015a0| 66 75 6c 20 66 75 6e 63 | 74 69 6f 6e 20 69 73 0d |ful func|tion is.|
|000015b0| 5c 63 6f 64 65 7b 65 72 | 72 5f 65 72 72 6e 6f 28 |\code{er|r_errno(|
|000015c0| 29 7d 2c 20 77 68 69 63 | 68 20 6f 6e 6c 79 20 74 |)}, whic|h only t|
|000015d0| 61 6b 65 73 20 61 6e 20 | 65 78 63 65 70 74 69 6f |akes an |exceptio|
|000015e0| 6e 20 61 72 67 75 6d 65 | 6e 74 20 61 6e 64 0d 63 |n argume|nt and.c|
|000015f0| 6f 6e 73 74 72 75 63 74 | 73 20 74 68 65 20 61 73 |onstruct|s the as|
|00001600| 73 6f 63 69 61 74 65 64 | 20 76 61 6c 75 65 20 62 |sociated| value b|
|00001610| 79 20 69 6e 73 70 65 63 | 74 69 6f 6e 20 6f 66 20 |y inspec|tion of |
|00001620| 74 68 65 20 28 55 4e 49 | 58 29 20 67 6c 6f 62 61 |the (UNI|X) globa|
|00001630| 6c 0d 76 61 72 69 61 62 | 6c 65 20 65 72 72 6e 6f |l.variab|le errno|
|00001640| 2e 20 20 54 68 65 20 6d | 6f 73 74 20 67 65 6e 65 |. The m|ost gene|
|00001650| 72 61 6c 20 66 75 6e 63 | 74 69 6f 6e 20 69 73 20 |ral func|tion is |
|00001660| 5c 63 6f 64 65 7b 65 72 | 72 5f 73 65 74 28 29 7d |\code{er|r_set()}|
|00001670| 2c 20 77 68 69 63 68 0d | 74 61 6b 65 73 20 74 77 |, which.|takes tw|
|00001680| 6f 20 6f 62 6a 65 63 74 | 20 61 72 67 75 6d 65 6e |o object| argumen|
|00001690| 74 73 2c 20 74 68 65 20 | 65 78 63 65 70 74 69 6f |ts, the |exceptio|
|000016a0| 6e 20 61 6e 64 20 69 74 | 73 20 61 73 73 6f 63 69 |n and it|s associ|
|000016b0| 61 74 65 64 20 76 61 6c | 75 65 2e 0d 59 6f 75 20 |ated val|ue..You |
|000016c0| 64 6f 6e 27 74 20 6e 65 | 65 64 20 74 6f 20 5c 63 |don't ne|ed to \c|
|000016d0| 6f 64 65 7b 49 4e 43 52 | 45 46 28 29 7d 20 74 68 |ode{INCR|EF()} th|
|000016e0| 65 20 6f 62 6a 65 63 74 | 73 20 70 61 73 73 65 64 |e object|s passed|
|000016f0| 20 74 6f 20 61 6e 79 20 | 6f 66 20 74 68 65 73 65 | to any |of these|
|00001700| 0d 66 75 6e 63 74 69 6f | 6e 73 2e 0d 0d 59 6f 75 |.functio|ns...You|
|00001710| 20 63 61 6e 20 74 65 73 | 74 20 6e 6f 6e 2d 64 65 | can tes|t non-de|
|00001720| 73 74 72 75 63 74 69 76 | 65 6c 79 20 77 68 65 74 |structiv|ely whet|
|00001730| 68 65 72 20 61 6e 20 65 | 78 63 65 70 74 69 6f 6e |her an e|xception|
|00001740| 20 68 61 73 20 62 65 65 | 6e 20 73 65 74 20 77 69 | has bee|n set wi|
|00001750| 74 68 0d 5c 63 6f 64 65 | 7b 65 72 72 5f 6f 63 63 |th.\code|{err_occ|
|00001760| 75 72 72 65 64 28 29 7d | 2e 20 20 48 6f 77 65 76 |urred()}|. Howev|
|00001770| 65 72 2c 20 6d 6f 73 74 | 20 63 6f 64 65 20 6e 65 |er, most| code ne|
|00001780| 76 65 72 20 63 61 6c 6c | 73 0d 5c 63 6f 64 65 7b |ver call|s.\code{|
|00001790| 65 72 72 5f 6f 63 63 75 | 72 72 65 64 28 29 7d 20 |err_occu|rred()} |
|000017a0| 74 6f 20 73 65 65 20 77 | 68 65 74 68 65 72 20 61 |to see w|hether a|
|000017b0| 6e 20 65 72 72 6f 72 20 | 6f 63 63 75 72 72 65 64 |n error |occurred|
|000017c0| 20 6f 72 20 6e 6f 74 2c | 20 62 75 74 0d 72 65 6c | or not,| but.rel|
|000017d0| 69 65 73 20 6f 6e 20 65 | 72 72 6f 72 20 72 65 74 |ies on e|rror ret|
|000017e0| 75 72 6e 20 76 61 6c 75 | 65 73 20 66 72 6f 6d 20 |urn valu|es from |
|000017f0| 74 68 65 20 66 75 6e 63 | 74 69 6f 6e 73 20 69 74 |the func|tions it|
|00001800| 20 63 61 6c 6c 73 20 69 | 6e 73 74 65 61 64 2e 0d | calls i|nstead..|
|00001810| 0d 57 68 65 6e 20 61 20 | 66 75 6e 63 74 69 6f 6e |.When a |function|
|00001820| 20 74 68 61 74 20 63 61 | 6c 6c 73 20 61 6e 6f 74 | that ca|lls anot|
|00001830| 68 65 72 20 66 75 6e 63 | 74 69 6f 6e 20 64 65 74 |her func|tion det|
|00001840| 65 63 74 73 20 74 68 61 | 74 20 74 68 65 20 63 61 |ects tha|t the ca|
|00001850| 6c 6c 65 64 0d 66 75 6e | 63 74 69 6f 6e 20 66 61 |lled.fun|ction fa|
|00001860| 69 6c 73 2c 20 69 74 20 | 73 68 6f 75 6c 64 20 72 |ils, it |should r|
|00001870| 65 74 75 72 6e 20 61 6e | 20 65 72 72 6f 72 20 76 |eturn an| error v|
|00001880| 61 6c 75 65 20 28 65 2e | 67 2e 20 5c 63 6f 64 65 |alue (e.|g. \code|
|00001890| 7b 4e 55 4c 4c 7d 20 6f | 72 0d 5c 63 6f 64 65 7b |{NULL} o|r.\code{|
|000018a0| 2d 31 7d 29 20 62 75 74 | 20 6e 6f 74 20 63 61 6c |-1}) but| not cal|
|000018b0| 6c 20 6f 6e 65 20 6f 66 | 20 74 68 65 20 5c 63 6f |l one of| the \co|
|000018c0| 64 65 7b 65 72 72 5f 2a | 7d 20 66 75 6e 63 74 69 |de{err_*|} functi|
|000018d0| 6f 6e 73 20 2d 2d 2d 20 | 6f 6e 65 20 68 61 73 0d |ons --- |one has.|
|000018e0| 61 6c 72 65 61 64 79 20 | 62 65 65 6e 20 63 61 6c |already |been cal|
|000018f0| 6c 65 64 2e 20 20 54 68 | 65 20 63 61 6c 6c 65 72 |led. Th|e caller|
|00001900| 20 69 73 20 74 68 65 6e | 20 73 75 70 70 6f 73 65 | is then| suppose|
|00001910| 64 20 74 6f 20 61 6c 73 | 6f 20 72 65 74 75 72 6e |d to als|o return|
|00001920| 20 61 6e 0d 65 72 72 6f | 72 20 69 6e 64 69 63 61 | an.erro|r indica|
|00001930| 74 69 6f 6e 20 74 6f 20 | 7b 5c 65 6d 20 69 74 73 |tion to |{\em its|
|00001940| 7d 20 63 61 6c 6c 65 72 | 2c 20 61 67 61 69 6e 20 |} caller|, again |
|00001950| 7b 5c 65 6d 20 77 69 74 | 68 6f 75 74 7d 20 63 61 |{\em wit|hout} ca|
|00001960| 6c 6c 69 6e 67 0d 5c 63 | 6f 64 65 7b 65 72 72 5f |lling.\c|ode{err_|
|00001970| 2a 28 29 7d 2c 20 61 6e | 64 20 73 6f 20 6f 6e 20 |*()}, an|d so on |
|00001980| 2d 2d 2d 20 74 68 65 20 | 6d 6f 73 74 20 64 65 74 |--- the |most det|
|00001990| 61 69 6c 65 64 20 63 61 | 75 73 65 20 6f 66 20 74 |ailed ca|use of t|
|000019a0| 68 65 20 65 72 72 6f 72 | 20 77 61 73 0d 61 6c 72 |he error| was.alr|
|000019b0| 65 61 64 79 20 72 65 70 | 6f 72 74 65 64 20 62 79 |eady rep|orted by|
|000019c0| 20 74 68 65 20 66 75 6e | 63 74 69 6f 6e 20 74 68 | the fun|ction th|
|000019d0| 61 74 20 66 69 72 73 74 | 20 64 65 74 65 63 74 65 |at first| detecte|
|000019e0| 64 20 69 74 2e 20 20 4f | 6e 63 65 20 74 68 65 0d |d it. O|nce the.|
|000019f0| 65 72 72 6f 72 20 68 61 | 73 20 72 65 61 63 68 65 |error ha|s reache|
|00001a00| 64 20 50 79 74 68 6f 6e | 27 73 20 69 6e 74 65 72 |d Python|'s inter|
|00001a10| 70 72 65 74 65 72 20 6d | 61 69 6e 20 6c 6f 6f 70 |preter m|ain loop|
|00001a20| 2c 20 74 68 69 73 20 61 | 62 6f 72 74 73 20 74 68 |, this a|borts th|
|00001a30| 65 0d 63 75 72 72 65 6e | 74 6c 79 20 65 78 65 63 |e.curren|tly exec|
|00001a40| 75 74 69 6e 67 20 50 79 | 74 68 6f 6e 20 63 6f 64 |uting Py|thon cod|
|00001a50| 65 20 61 6e 64 20 74 72 | 69 65 73 20 74 6f 20 66 |e and tr|ies to f|
|00001a60| 69 6e 64 20 61 6e 20 65 | 78 63 65 70 74 69 6f 6e |ind an e|xception|
|00001a70| 20 68 61 6e 64 6c 65 72 | 0d 73 70 65 63 69 66 69 | handler|.specifi|
|00001a80| 65 64 20 62 79 20 74 68 | 65 20 50 79 74 68 6f 6e |ed by th|e Python|
|00001a90| 20 70 72 6f 67 72 61 6d | 6d 65 72 2e 0d 0d 28 54 | program|mer...(T|
|00001aa0| 68 65 72 65 20 61 72 65 | 20 73 69 74 75 61 74 69 |here are| situati|
|00001ab0| 6f 6e 73 20 77 68 65 72 | 65 20 61 20 6d 6f 64 75 |ons wher|e a modu|
|00001ac0| 6c 65 20 63 61 6e 20 61 | 63 74 75 61 6c 6c 79 20 |le can a|ctually |
|00001ad0| 67 69 76 65 20 61 20 6d | 6f 72 65 20 64 65 74 61 |give a m|ore deta|
|00001ae0| 69 6c 65 64 0d 65 72 72 | 6f 72 20 6d 65 73 73 61 |iled.err|or messa|
|00001af0| 67 65 20 62 79 20 63 61 | 6c 6c 69 6e 67 20 61 6e |ge by ca|lling an|
|00001b00| 6f 74 68 65 72 20 5c 63 | 6f 64 65 7b 65 72 72 5f |other \c|ode{err_|
|00001b10| 2a 7d 20 66 75 6e 63 74 | 69 6f 6e 2c 20 61 6e 64 |*} funct|ion, and|
|00001b20| 20 69 6e 20 73 75 63 68 | 0d 63 61 73 65 73 20 69 | in such|.cases i|
|00001b30| 74 20 69 73 20 66 69 6e | 65 20 74 6f 20 64 6f 20 |t is fin|e to do |
|00001b40| 73 6f 2e 20 20 41 73 20 | 61 20 67 65 6e 65 72 61 |so. As |a genera|
|00001b50| 6c 20 72 75 6c 65 2c 20 | 68 6f 77 65 76 65 72 2c |l rule, |however,|
|00001b60| 20 74 68 69 73 20 69 73 | 20 6e 6f 74 0d 6e 65 63 | this is| not.nec|
|00001b70| 65 73 73 61 72 79 2c 20 | 61 6e 64 20 63 61 6e 20 |essary, |and can |
|00001b80| 63 61 75 73 65 20 69 6e | 66 6f 72 6d 61 74 69 6f |cause in|formatio|
|00001b90| 6e 20 61 62 6f 75 74 20 | 74 68 65 20 63 61 75 73 |n about |the caus|
|00001ba0| 65 20 6f 66 20 74 68 65 | 20 65 72 72 6f 72 20 74 |e of the| error t|
|00001bb0| 6f 0d 62 65 20 6c 6f 73 | 74 3a 20 6d 6f 73 74 20 |o.be los|t: most |
|00001bc0| 6f 70 65 72 61 74 69 6f | 6e 73 20 63 61 6e 20 66 |operatio|ns can f|
|00001bd0| 61 69 6c 20 66 6f 72 20 | 61 20 76 61 72 69 65 74 |ail for |a variet|
|00001be0| 79 20 6f 66 20 72 65 61 | 73 6f 6e 73 2e 29 0d 0d |y of rea|sons.)..|
|00001bf0| 54 6f 20 69 67 6e 6f 72 | 65 20 61 6e 20 65 78 63 |To ignor|e an exc|
|00001c00| 65 70 74 69 6f 6e 20 73 | 65 74 20 62 79 20 61 20 |eption s|et by a |
|00001c10| 66 75 6e 63 74 69 6f 6e | 20 63 61 6c 6c 20 74 68 |function| call th|
|00001c20| 61 74 20 66 61 69 6c 65 | 64 2c 20 74 68 65 0d 65 |at faile|d, the.e|
|00001c30| 78 63 65 70 74 69 6f 6e | 20 63 6f 6e 64 69 74 69 |xception| conditi|
|00001c40| 6f 6e 20 6d 75 73 74 20 | 62 65 20 63 6c 65 61 72 |on must |be clear|
|00001c50| 65 64 20 65 78 70 6c 69 | 63 69 74 6c 79 20 62 79 |ed expli|citly by|
|00001c60| 20 63 61 6c 6c 69 6e 67 | 0d 5c 63 6f 64 65 7b 65 | calling|.\code{e|
|00001c70| 72 72 5f 63 6c 65 61 72 | 28 29 7d 2e 20 20 54 68 |rr_clear|()}. Th|
|00001c80| 65 20 6f 6e 6c 79 20 74 | 69 6d 65 20 43 20 63 6f |e only t|ime C co|
|00001c90| 64 65 20 73 68 6f 75 6c | 64 20 63 61 6c 6c 0d 5c |de shoul|d call.\|
|00001ca0| 63 6f 64 65 7b 65 72 72 | 5f 63 6c 65 61 72 28 29 |code{err|_clear()|
|00001cb0| 7d 20 69 73 20 69 66 20 | 69 74 20 64 6f 65 73 6e |} is if |it doesn|
|00001cc0| 27 74 20 77 61 6e 74 20 | 74 6f 20 70 61 73 73 20 |'t want |to pass |
|00001cd0| 74 68 65 20 65 72 72 6f | 72 20 6f 6e 20 74 6f 20 |the erro|r on to |
|00001ce0| 74 68 65 0d 69 6e 74 65 | 72 70 72 65 74 65 72 20 |the.inte|rpreter |
|00001cf0| 62 75 74 20 77 61 6e 74 | 73 20 74 6f 20 68 61 6e |but want|s to han|
|00001d00| 64 6c 65 20 69 74 20 63 | 6f 6d 70 6c 65 74 65 6c |dle it c|ompletel|
|00001d10| 79 20 62 79 20 69 74 73 | 65 6c 66 20 28 65 2e 67 |y by its|elf (e.g|
|00001d20| 2e 20 62 79 0d 74 72 79 | 69 6e 67 20 73 6f 6d 65 |. by.try|ing some|
|00001d30| 74 68 69 6e 67 20 65 6c | 73 65 20 6f 72 20 70 72 |thing el|se or pr|
|00001d40| 65 74 65 6e 64 69 6e 67 | 20 6e 6f 74 68 69 6e 67 |etending| nothing|
|00001d50| 20 68 61 70 70 65 6e 65 | 64 29 2e 0d 0d 46 69 6e | happene|d)...Fin|
|00001d60| 61 6c 6c 79 2c 20 74 68 | 65 20 66 75 6e 63 74 69 |ally, th|e functi|
|00001d70| 6f 6e 20 5c 63 6f 64 65 | 7b 65 72 72 5f 67 65 74 |on \code|{err_get|
|00001d80| 28 29 7d 20 67 69 76 65 | 73 20 79 6f 75 20 62 6f |()} give|s you bo|
|00001d90| 74 68 20 65 72 72 6f 72 | 20 76 61 72 69 61 62 6c |th error| variabl|
|00001da0| 65 73 0d 7b 5c 65 6d 20 | 61 6e 64 20 63 6c 65 61 |es.{\em |and clea|
|00001db0| 72 73 20 74 68 65 6d 7d | 2e 20 20 4e 6f 74 65 20 |rs them}|. Note |
|00001dc0| 74 68 61 74 20 65 76 65 | 6e 20 69 66 20 61 6e 20 |that eve|n if an |
|00001dd0| 65 72 72 6f 72 20 6f 63 | 63 75 72 72 65 64 20 74 |error oc|curred t|
|00001de0| 68 65 20 73 65 63 6f 6e | 64 0d 6f 6e 65 20 6d 61 |he secon|d.one ma|
|00001df0| 79 20 62 65 20 5c 63 6f | 64 65 7b 4e 55 4c 4c 7d |y be \co|de{NULL}|
|00001e00| 2e 20 20 59 6f 75 20 68 | 61 76 65 20 74 6f 20 5c |. You h|ave to \|
|00001e10| 63 6f 64 65 7b 58 44 45 | 43 52 45 46 28 29 7d 20 |code{XDE|CREF()} |
|00001e20| 62 6f 74 68 20 77 68 65 | 6e 20 79 6f 75 0d 61 72 |both whe|n you.ar|
|00001e30| 65 20 66 69 6e 69 73 68 | 65 64 20 77 69 74 68 20 |e finish|ed with |
|00001e40| 74 68 65 6d 2e 20 20 49 | 20 64 6f 75 62 74 20 79 |them. I| doubt y|
|00001e50| 6f 75 20 77 69 6c 6c 20 | 6e 65 65 64 20 74 6f 20 |ou will |need to |
|00001e60| 75 73 65 20 74 68 69 73 | 20 66 75 6e 63 74 69 6f |use this| functio|
|00001e70| 6e 2e 0d 0d 4e 6f 74 65 | 20 74 68 61 74 20 61 20 |n...Note| that a |
|00001e80| 66 61 69 6c 69 6e 67 20 | 5c 63 6f 64 65 7b 6d 61 |failing |\code{ma|
|00001e90| 6c 6c 6f 63 28 29 7d 20 | 63 61 6c 6c 20 6d 75 73 |lloc()} |call mus|
|00001ea0| 74 20 61 6c 73 6f 20 62 | 65 20 74 75 72 6e 65 64 |t also b|e turned|
|00001eb0| 20 69 6e 74 6f 20 61 6e | 0d 65 78 63 65 70 74 69 | into an|.excepti|
|00001ec0| 6f 6e 20 2d 2d 2d 20 74 | 68 65 20 64 69 72 65 63 |on --- t|he direc|
|00001ed0| 74 20 63 61 6c 6c 65 72 | 20 6f 66 20 5c 63 6f 64 |t caller| of \cod|
|00001ee0| 65 7b 6d 61 6c 6c 6f 63 | 28 29 7d 20 28 6f 72 0d |e{malloc|()} (or.|
|00001ef0| 5c 63 6f 64 65 7b 72 65 | 61 6c 6c 6f 63 28 29 7d |\code{re|alloc()}|
|00001f00| 29 20 6d 75 73 74 20 63 | 61 6c 6c 20 5c 63 6f 64 |) must c|all \cod|
|00001f10| 65 7b 65 72 72 5f 6e 6f | 6d 65 6d 28 29 7d 20 61 |e{err_no|mem()} a|
|00001f20| 6e 64 20 72 65 74 75 72 | 6e 20 61 20 66 61 69 6c |nd retur|n a fail|
|00001f30| 75 72 65 0d 69 6e 64 69 | 63 61 74 6f 72 20 69 74 |ure.indi|cator it|
|00001f40| 73 65 6c 66 2e 20 20 41 | 6c 6c 20 74 68 65 20 6f |self. A|ll the o|
|00001f50| 62 6a 65 63 74 2d 63 72 | 65 61 74 69 6e 67 20 66 |bject-cr|eating f|
|00001f60| 75 6e 63 74 69 6f 6e 73 | 0d 28 5c 63 6f 64 65 7b |unctions|.(\code{|
|00001f70| 6e 65 77 69 6e 74 6f 62 | 6a 65 63 74 28 29 7d 20 |newintob|ject()} |
|00001f80| 65 74 63 2e 29 20 61 6c | 72 65 61 64 79 20 64 6f |etc.) al|ready do|
|00001f90| 20 74 68 69 73 2c 20 73 | 6f 20 6f 6e 6c 79 20 69 | this, s|o only i|
|00001fa0| 66 20 79 6f 75 20 63 61 | 6c 6c 0d 5c 63 6f 64 65 |f you ca|ll.\code|
|00001fb0| 7b 6d 61 6c 6c 6f 63 28 | 29 7d 20 64 69 72 65 63 |{malloc(|)} direc|
|00001fc0| 74 6c 79 20 74 68 69 73 | 20 6e 6f 74 65 20 69 73 |tly this| note is|
|00001fd0| 20 6f 66 20 69 6d 70 6f | 72 74 61 6e 63 65 2e 0d | of impo|rtance..|
|00001fe0| 0d 41 6c 73 6f 20 6e 6f | 74 65 20 74 68 61 74 2c |.Also no|te that,|
|00001ff0| 20 77 69 74 68 20 74 68 | 65 20 69 6d 70 6f 72 74 | with th|e import|
|00002000| 61 6e 74 20 65 78 63 65 | 70 74 69 6f 6e 20 6f 66 |ant exce|ption of|
|00002010| 20 5c 63 6f 64 65 7b 67 | 65 74 61 72 67 73 28 29 | \code{g|etargs()|
|00002020| 7d 2c 0d 66 75 6e 63 74 | 69 6f 6e 73 20 74 68 61 |},.funct|ions tha|
|00002030| 74 20 72 65 74 75 72 6e | 20 61 6e 20 69 6e 74 65 |t return| an inte|
|00002040| 67 65 72 20 73 74 61 74 | 75 73 20 75 73 75 61 6c |ger stat|us usual|
|00002050| 6c 79 20 72 65 74 75 72 | 6e 20 5c 63 6f 64 65 7b |ly retur|n \code{|
|00002060| 30 7d 20 6f 72 20 61 0d | 70 6f 73 69 74 69 76 65 |0} or a.|positive|
|00002070| 20 76 61 6c 75 65 20 66 | 6f 72 20 73 75 63 63 65 | value f|or succe|
|00002080| 73 73 20 61 6e 64 20 5c | 63 6f 64 65 7b 2d 31 7d |ss and \|code{-1}|
|00002090| 20 66 6f 72 20 66 61 69 | 6c 75 72 65 2e 0d 0d 46 | for fai|lure...F|
|000020a0| 69 6e 61 6c 6c 79 2c 20 | 62 65 20 63 61 72 65 66 |inally, |be caref|
|000020b0| 75 6c 20 61 62 6f 75 74 | 20 63 6c 65 61 6e 69 6e |ul about| cleanin|
|000020c0| 67 20 75 70 20 67 61 72 | 62 61 67 65 20 28 6d 61 |g up gar|bage (ma|
|000020d0| 6b 69 6e 67 20 5c 63 6f | 64 65 7b 58 44 45 43 52 |king \co|de{XDECR|
|000020e0| 45 46 28 29 7d 0d 6f 72 | 20 5c 63 6f 64 65 7b 44 |EF()}.or| \code{D|
|000020f0| 45 43 52 45 46 28 29 7d | 20 63 61 6c 6c 73 20 66 |ECREF()}| calls f|
|00002100| 6f 72 20 6f 62 6a 65 63 | 74 73 20 79 6f 75 20 68 |or objec|ts you h|
|00002110| 61 76 65 20 61 6c 72 65 | 61 64 79 20 63 72 65 61 |ave alre|ady crea|
|00002120| 74 65 64 29 20 77 68 65 | 6e 0d 79 6f 75 20 72 65 |ted) whe|n.you re|
|00002130| 74 75 72 6e 20 61 6e 20 | 65 72 72 6f 72 21 0d 0d |turn an |error!..|
|00002140| 54 68 65 20 63 68 6f 69 | 63 65 20 6f 66 20 77 68 |The choi|ce of wh|
|00002150| 69 63 68 20 65 78 63 65 | 70 74 69 6f 6e 20 74 6f |ich exce|ption to|
|00002160| 20 72 61 69 73 65 20 69 | 73 20 65 6e 74 69 72 65 | raise i|s entire|
|00002170| 6c 79 20 79 6f 75 72 73 | 2e 20 20 54 68 65 72 65 |ly yours|. There|
|00002180| 20 61 72 65 0d 70 72 65 | 64 65 63 6c 61 72 65 64 | are.pre|declared|
|00002190| 20 43 20 6f 62 6a 65 63 | 74 73 20 63 6f 72 72 65 | C objec|ts corre|
|000021a0| 73 70 6f 6e 64 69 6e 67 | 20 74 6f 20 61 6c 6c 20 |sponding| to all |
|000021b0| 62 75 69 6c 74 2d 69 6e | 20 50 79 74 68 6f 6e 20 |built-in| Python |
|000021c0| 65 78 63 65 70 74 69 6f | 6e 73 2c 0d 65 2e 67 2e |exceptio|ns,.e.g.|
|000021d0| 20 5c 63 6f 64 65 7b 5a | 65 72 6f 44 65 76 69 73 | \code{Z|eroDevis|
|000021e0| 69 6f 6e 45 72 72 6f 72 | 7d 20 77 68 69 63 68 20 |ionError|} which |
|000021f0| 79 6f 75 20 63 61 6e 20 | 75 73 65 20 64 69 72 65 |you can |use dire|
|00002200| 63 74 6c 79 2e 20 20 4f | 66 20 63 6f 75 72 73 65 |ctly. O|f course|
|00002210| 2c 0d 79 6f 75 20 73 68 | 6f 75 6c 64 20 63 68 6f |,.you sh|ould cho|
|00002220| 73 65 20 65 78 63 65 70 | 74 69 6f 6e 73 20 77 69 |se excep|tions wi|
|00002230| 73 65 6c 79 20 2d 2d 2d | 20 64 6f 6e 27 74 20 75 |sely ---| don't u|
|00002240| 73 65 20 5c 63 6f 64 65 | 7b 54 79 70 65 45 72 72 |se \code|{TypeErr|
|00002250| 6f 72 7d 20 74 6f 0d 6d | 65 61 6e 20 74 68 61 74 |or} to.m|ean that|
|00002260| 20 61 20 66 69 6c 65 20 | 63 6f 75 6c 64 6e 27 74 | a file |couldn't|
|00002270| 20 62 65 20 6f 70 65 6e | 65 64 20 28 74 68 61 74 | be open|ed (that|
|00002280| 20 73 68 6f 75 6c 64 20 | 70 72 6f 62 61 62 6c 79 | should |probably|
|00002290| 20 62 65 0d 5c 63 6f 64 | 65 7b 49 4f 45 72 72 6f | be.\cod|e{IOErro|
|000022a0| 72 7d 29 2e 20 20 49 66 | 20 61 6e 79 74 68 69 6e |r}). If| anythin|
|000022b0| 67 27 73 20 77 72 6f 6e | 67 20 77 69 74 68 20 74 |g's wron|g with t|
|000022c0| 68 65 20 61 72 67 75 6d | 65 6e 74 20 6c 69 73 74 |he argum|ent list|
|000022d0| 20 74 68 65 0d 5c 63 6f | 64 65 7b 67 65 74 61 72 | the.\co|de{getar|
|000022e0| 67 73 28 29 7d 20 66 75 | 6e 63 74 69 6f 6e 20 72 |gs()} fu|nction r|
|000022f0| 61 69 73 65 73 20 5c 63 | 6f 64 65 7b 54 79 70 65 |aises \c|ode{Type|
|00002300| 45 72 72 6f 72 7d 2e 20 | 20 49 66 20 79 6f 75 20 |Error}. | If you |
|00002310| 68 61 76 65 20 61 6e 0d | 61 72 67 75 6d 65 6e 74 |have an.|argument|
|00002320| 20 77 68 6f 73 65 20 76 | 61 6c 75 65 20 77 68 69 | whose v|alue whi|
|00002330| 63 68 20 6d 75 73 74 20 | 62 65 20 69 6e 20 61 20 |ch must |be in a |
|00002340| 70 61 72 74 69 63 75 6c | 61 72 20 72 61 6e 67 65 |particul|ar range|
|00002350| 20 6f 72 20 6d 75 73 74 | 0d 73 61 74 69 73 66 79 | or must|.satisfy|
|00002360| 20 6f 74 68 65 72 20 63 | 6f 6e 64 69 74 69 6f 6e | other c|ondition|
|00002370| 73 2c 20 5c 63 6f 64 65 | 7b 56 61 6c 75 65 45 72 |s, \code|{ValueEr|
|00002380| 72 6f 72 7d 20 69 73 20 | 61 70 70 72 6f 70 72 69 |ror} is |appropri|
|00002390| 61 74 65 2e 0d 0d 59 6f | 75 20 63 61 6e 20 61 6c |ate...Yo|u can al|
|000023a0| 73 6f 20 64 65 66 69 6e | 65 20 61 20 6e 65 77 20 |so defin|e a new |
|000023b0| 65 78 63 65 70 74 69 6f | 6e 20 74 68 61 74 20 69 |exceptio|n that i|
|000023c0| 73 20 75 6e 69 71 75 65 | 20 74 6f 20 79 6f 75 72 |s unique| to your|
|000023d0| 20 6d 6f 64 75 6c 65 2e | 0d 46 6f 72 20 74 68 69 | module.|.For thi|
|000023e0| 73 2c 20 79 6f 75 20 75 | 73 75 61 6c 6c 79 20 64 |s, you u|sually d|
|000023f0| 65 63 6c 61 72 65 20 61 | 20 73 74 61 74 69 63 20 |eclare a| static |
|00002400| 6f 62 6a 65 63 74 20 76 | 61 72 69 61 62 6c 65 20 |object v|ariable |
|00002410| 61 74 20 74 68 65 0d 62 | 65 67 69 6e 6e 69 6e 67 |at the.b|eginning|
|00002420| 20 6f 66 20 79 6f 75 72 | 20 66 69 6c 65 2c 20 65 | of your| file, e|
|00002430| 2e 67 2e 0d 0d 5c 62 65 | 67 69 6e 7b 76 65 72 62 |.g...\be|gin{verb|
|00002440| 61 74 69 6d 7d 0d 20 20 | 20 20 73 74 61 74 69 63 |atim}. | static|
|00002450| 20 6f 62 6a 65 63 74 20 | 2a 46 6f 6f 45 72 72 6f | object |*FooErro|
|00002460| 72 3b 0d 5c 65 6e 64 7b | 76 65 72 62 61 74 69 6d |r;.\end{|verbatim|
|00002470| 7d 0d 0d 61 6e 64 20 69 | 6e 69 74 69 61 6c 69 7a |}..and i|nitializ|
|00002480| 65 20 69 74 20 69 6e 20 | 79 6f 75 72 20 6d 6f 64 |e it in |your mod|
|00002490| 75 6c 65 27 73 20 69 6e | 69 74 69 61 6c 69 7a 61 |ule's in|itializa|
|000024a0| 74 69 6f 6e 20 66 75 6e | 63 74 69 6f 6e 0d 28 5c |tion fun|ction.(\|
|000024b0| 63 6f 64 65 7b 69 6e 69 | 74 66 6f 6f 28 29 7d 29 |code{ini|tfoo()})|
|000024c0| 20 77 69 74 68 20 61 20 | 73 74 72 69 6e 67 20 6f | with a |string o|
|000024d0| 62 6a 65 63 74 2c 20 65 | 2e 67 2e 20 28 6c 65 61 |bject, e|.g. (lea|
|000024e0| 76 69 6e 67 20 6f 75 74 | 20 74 68 65 20 65 72 72 |ving out| the err|
|000024f0| 6f 72 0d 63 68 65 63 6b | 69 6e 67 20 66 6f 72 20 |or.check|ing for |
|00002500| 73 69 6d 70 6c 69 63 69 | 74 79 29 3a 0d 0d 5c 62 |simplici|ty):..\b|
|00002510| 65 67 69 6e 7b 76 65 72 | 62 61 74 69 6d 7d 0d 20 |egin{ver|batim}. |
|00002520| 20 20 20 76 6f 69 64 0d | 20 20 20 20 69 6e 69 74 | void.| init|
|00002530| 66 6f 6f 28 29 0d 20 20 | 20 20 7b 0d 20 20 20 20 |foo(). | {. |
|00002540| 20 20 20 20 6f 62 6a 65 | 63 74 20 2a 6d 2c 20 2a | obje|ct *m, *|
|00002550| 64 3b 0d 20 20 20 20 20 | 20 20 20 6d 20 3d 20 69 |d;. | m = i|
|00002560| 6e 69 74 6d 6f 64 75 6c | 65 28 22 66 6f 6f 22 2c |nitmodul|e("foo",|
|00002570| 20 66 6f 6f 5f 6d 65 74 | 68 6f 64 73 29 3b 0d 20 | foo_met|hods);. |
|00002580| 20 20 20 20 20 20 20 64 | 20 3d 20 67 65 74 6d 6f | d| = getmo|
|00002590| 64 75 6c 65 64 69 63 74 | 28 6d 29 3b 0d 20 20 20 |duledict|(m);. |
|000025a0| 20 20 20 20 20 46 6f 6f | 45 72 72 6f 72 20 3d 20 | Foo|Error = |
|000025b0| 6e 65 77 73 74 72 69 6e | 67 6f 62 6a 65 63 74 28 |newstrin|gobject(|
|000025c0| 22 66 6f 6f 2e 65 72 72 | 6f 72 22 29 3b 0d 20 20 |"foo.err|or");. |
|000025d0| 20 20 20 20 20 20 64 69 | 63 74 69 6e 73 65 72 74 | di|ctinsert|
|000025e0| 28 64 2c 20 22 65 72 72 | 6f 72 22 2c 20 46 6f 6f |(d, "err|or", Foo|
|000025f0| 45 72 72 6f 72 29 3b 0d | 20 20 20 20 7d 0d 5c 65 |Error);.| }.\e|
|00002600| 6e 64 7b 76 65 72 62 61 | 74 69 6d 7d 0d 0d 0d 5c |nd{verba|tim}...\|
|00002610| 73 65 63 74 69 6f 6e 7b | 42 61 63 6b 20 74 6f 20 |section{|Back to |
|00002620| 74 68 65 20 65 78 61 6d | 70 6c 65 7d 0d 0d 47 6f |the exam|ple}..Go|
|00002630| 69 6e 67 20 62 61 63 6b | 20 74 6f 20 5c 63 6f 64 |ing back| to \cod|
|00002640| 65 7b 70 6f 73 69 78 5f | 73 79 73 74 65 6d 28 29 |e{posix_|system()|
|00002650| 7d 2c 20 79 6f 75 20 73 | 68 6f 75 6c 64 20 6e 6f |}, you s|hould no|
|00002660| 77 20 62 65 20 61 62 6c | 65 20 74 6f 0d 75 6e 64 |w be abl|e to.und|
|00002670| 65 72 73 74 61 6e 64 20 | 74 68 69 73 20 62 69 74 |erstand |this bit|
|00002680| 3a 0d 0d 5c 62 65 67 69 | 6e 7b 76 65 72 62 61 74 |:..\begi|n{verbat|
|00002690| 69 6d 7d 0d 20 20 20 20 | 20 20 20 20 69 66 20 28 |im}. | if (|
|000026a0| 21 67 65 74 61 72 67 73 | 28 61 72 67 73 2c 20 22 |!getargs|(args, "|
|000026b0| 73 22 2c 20 26 63 6f 6d | 6d 61 6e 64 29 29 0d 20 |s", &com|mand)). |
|000026c0| 20 20 20 20 20 20 20 20 | 20 20 20 72 65 74 75 72 | | retur|
|000026d0| 6e 20 4e 55 4c 4c 3b 0d | 5c 65 6e 64 7b 76 65 72 |n NULL;.|\end{ver|
|000026e0| 62 61 74 69 6d 7d 0d 0d | 49 74 20 72 65 74 75 72 |batim}..|It retur|
|000026f0| 6e 73 20 5c 63 6f 64 65 | 7b 4e 55 4c 4c 7d 20 28 |ns \code|{NULL} (|
|00002700| 74 68 65 20 65 72 72 6f | 72 20 69 6e 64 69 63 61 |the erro|r indica|
|00002710| 74 6f 72 20 66 6f 72 20 | 66 75 6e 63 74 69 6f 6e |tor for |function|
|00002720| 73 20 6f 66 20 74 68 69 | 73 0d 6b 69 6e 64 29 20 |s of thi|s.kind) |
|00002730| 69 66 20 61 6e 20 65 72 | 72 6f 72 20 69 73 20 64 |if an er|ror is d|
|00002740| 65 74 65 63 74 65 64 20 | 69 6e 20 74 68 65 20 61 |etected |in the a|
|00002750| 72 67 75 6d 65 6e 74 20 | 6c 69 73 74 2c 20 72 65 |rgument |list, re|
|00002760| 6c 79 69 6e 67 20 6f 6e | 20 74 68 65 0d 65 78 63 |lying on| the.exc|
|00002770| 65 70 74 69 6f 6e 20 73 | 65 74 20 62 79 20 5c 63 |eption s|et by \c|
|00002780| 6f 64 65 7b 67 65 74 61 | 72 67 73 28 29 7d 2e 20 |ode{geta|rgs()}. |
|00002790| 20 4f 74 68 65 72 77 69 | 73 65 20 74 68 65 20 73 | Otherwi|se the s|
|000027a0| 74 72 69 6e 67 20 76 61 | 6c 75 65 20 6f 66 20 74 |tring va|lue of t|
|000027b0| 68 65 0d 61 72 67 75 6d | 65 6e 74 20 68 61 73 20 |he.argum|ent has |
|000027c0| 62 65 65 6e 20 63 6f 70 | 69 65 64 20 74 6f 20 74 |been cop|ied to t|
|000027d0| 68 65 20 6c 6f 63 61 6c | 20 76 61 72 69 61 62 6c |he local| variabl|
|000027e0| 65 20 5c 63 6f 64 65 7b | 63 6f 6d 6d 61 6e 64 7d |e \code{|command}|
|000027f0| 20 2d 2d 2d 20 74 68 69 | 73 0d 69 73 20 69 6e 20 | --- thi|s.is in |
|00002800| 66 61 63 74 20 6a 75 73 | 74 20 61 20 70 6f 69 6e |fact jus|t a poin|
|00002810| 74 65 72 20 61 73 73 69 | 67 6e 6d 65 6e 74 20 61 |ter assi|gnment a|
|00002820| 6e 64 20 79 6f 75 20 61 | 72 65 20 6e 6f 74 20 73 |nd you a|re not s|
|00002830| 75 70 70 6f 73 65 64 20 | 74 6f 0d 6d 6f 64 69 66 |upposed |to.modif|
|00002840| 79 20 74 68 65 20 73 74 | 72 69 6e 67 20 74 6f 20 |y the st|ring to |
|00002850| 77 68 69 63 68 20 69 74 | 20 70 6f 69 6e 74 73 2e |which it| points.|
|00002860| 0d 0d 49 66 20 61 20 66 | 75 6e 63 74 69 6f 6e 20 |..If a f|unction |
|00002870| 69 73 20 63 61 6c 6c 65 | 64 20 77 69 74 68 20 6d |is calle|d with m|
|00002880| 75 6c 74 69 70 6c 65 20 | 61 72 67 75 6d 65 6e 74 |ultiple |argument|
|00002890| 73 2c 20 74 68 65 20 61 | 72 67 75 6d 65 6e 74 20 |s, the a|rgument |
|000028a0| 6c 69 73 74 0d 28 74 68 | 65 20 61 72 67 75 6d 65 |list.(th|e argume|
|000028b0| 6e 74 20 5c 63 6f 64 65 | 7b 61 72 67 73 7d 29 20 |nt \code|{args}) |
|000028c0| 69 73 20 74 75 72 6e 65 | 64 20 69 6e 74 6f 20 61 |is turne|d into a|
|000028d0| 20 74 75 70 6c 65 2e 20 | 20 49 66 20 69 74 20 69 | tuple. | If it i|
|000028e0| 73 20 63 61 6c 6c 65 64 | 0d 77 69 74 68 6f 75 74 |s called|.without|
|000028f0| 20 61 72 67 75 6d 65 6e | 74 73 2c 20 5c 63 6f 64 | argumen|ts, \cod|
|00002900| 65 7b 61 72 67 73 7d 20 | 69 73 20 5c 63 6f 64 65 |e{args} |is \code|
|00002910| 7b 4e 55 4c 4c 7d 2e 20 | 5c 63 6f 64 65 7b 67 65 |{NULL}. |\code{ge|
|00002920| 74 61 72 67 73 28 29 7d | 20 6b 6e 6f 77 73 0d 61 |targs()}| knows.a|
|00002930| 62 6f 75 74 20 74 68 69 | 73 3b 20 73 65 65 20 6c |bout thi|s; see l|
|00002940| 61 74 65 72 2e 0d 0d 54 | 68 65 20 6e 65 78 74 20 |ater...T|he next |
|00002950| 73 74 61 74 65 6d 65 6e | 74 20 69 6e 20 5c 63 6f |statemen|t in \co|
|00002960| 64 65 7b 70 6f 73 69 78 | 5f 73 79 73 74 65 6d 28 |de{posix|_system(|
|00002970| 29 7d 20 69 73 20 61 20 | 63 61 6c 6c 20 74 6f 20 |)} is a |call to |
|00002980| 74 68 65 20 43 20 6c 69 | 62 72 61 72 79 0d 66 75 |the C li|brary.fu|
|00002990| 6e 63 74 69 6f 6e 20 5c | 63 6f 64 65 7b 73 79 73 |nction \|code{sys|
|000029a0| 74 65 6d 28 29 7d 2c 20 | 70 61 73 73 69 6e 67 20 |tem()}, |passing |
|000029b0| 69 74 20 74 68 65 20 73 | 74 72 69 6e 67 20 77 65 |it the s|tring we|
|000029c0| 20 6a 75 73 74 20 67 6f | 74 20 66 72 6f 6d 0d 5c | just go|t from.\|
|000029d0| 63 6f 64 65 7b 67 65 74 | 61 72 67 73 28 29 7d 3a |code{get|args()}:|
|000029e0| 0d 0d 5c 62 65 67 69 6e | 7b 76 65 72 62 61 74 69 |..\begin|{verbati|
|000029f0| 6d 7d 0d 20 20 20 20 20 | 20 20 20 73 74 73 20 3d |m}. | sts =|
|00002a00| 20 73 79 73 74 65 6d 28 | 63 6f 6d 6d 61 6e 64 29 | system(|command)|
|00002a10| 3b 0d 5c 65 6e 64 7b 76 | 65 72 62 61 74 69 6d 7d |;.\end{v|erbatim}|
|00002a20| 0d 0d 46 69 6e 61 6c 6c | 79 2c 20 5c 63 6f 64 65 |..Finall|y, \code|
|00002a30| 7b 70 6f 73 69 78 2e 73 | 79 73 74 65 6d 28 29 7d |{posix.s|ystem()}|
|00002a40| 20 6d 75 73 74 20 72 65 | 74 75 72 6e 20 61 20 76 | must re|turn a v|
|00002a50| 61 6c 75 65 3a 20 74 68 | 65 20 69 6e 74 65 67 65 |alue: th|e intege|
|00002a60| 72 20 73 74 61 74 75 73 | 0d 72 65 74 75 72 6e 65 |r status|.returne|
|00002a70| 64 20 62 79 20 74 68 65 | 20 43 20 6c 69 62 72 61 |d by the| C libra|
|00002a80| 72 79 20 5c 63 6f 64 65 | 7b 73 79 73 74 65 6d 28 |ry \code|{system(|
|00002a90| 29 7d 20 66 75 6e 63 74 | 69 6f 6e 2e 20 20 54 68 |)} funct|ion. Th|
|00002aa0| 69 73 20 69 73 20 64 6f | 6e 65 0d 75 73 69 6e 67 |is is do|ne.using|
|00002ab0| 20 74 68 65 20 66 75 6e | 63 74 69 6f 6e 20 5c 63 | the fun|ction \c|
|00002ac0| 6f 64 65 7b 6d 6b 76 61 | 6c 75 65 28 29 7d 2c 20 |ode{mkva|lue()}, |
|00002ad0| 77 68 69 63 68 20 69 73 | 20 73 6f 6d 65 74 68 69 |which is| somethi|
|00002ae0| 6e 67 20 6c 69 6b 65 20 | 74 68 65 0d 69 6e 76 65 |ng like |the.inve|
|00002af0| 72 73 65 20 6f 66 20 5c | 63 6f 64 65 7b 67 65 74 |rse of \|code{get|
|00002b00| 61 72 67 73 28 29 7d 3a | 20 69 74 20 74 61 6b 65 |args()}:| it take|
|00002b10| 73 20 61 20 66 6f 72 6d | 61 74 20 73 74 72 69 6e |s a form|at strin|
|00002b20| 67 20 61 6e 64 20 61 20 | 76 61 72 69 61 62 6c 65 |g and a |variable|
|00002b30| 0d 6e 75 6d 62 65 72 20 | 6f 66 20 43 20 76 61 6c |.number |of C val|
|00002b40| 75 65 73 20 61 6e 64 20 | 72 65 74 75 72 6e 73 20 |ues and |returns |
|00002b50| 61 20 6e 65 77 20 50 79 | 74 68 6f 6e 20 6f 62 6a |a new Py|thon obj|
|00002b60| 65 63 74 2e 0d 0d 5c 62 | 65 67 69 6e 7b 76 65 72 |ect...\b|egin{ver|
|00002b70| 62 61 74 69 6d 7d 0d 20 | 20 20 20 20 20 20 20 72 |batim}. | r|
|00002b80| 65 74 75 72 6e 20 6d 6b | 76 61 6c 75 65 28 22 69 |eturn mk|value("i|
|00002b90| 22 2c 20 73 74 73 29 3b | 0d 5c 65 6e 64 7b 76 65 |", sts);|.\end{ve|
|00002ba0| 72 62 61 74 69 6d 7d 0d | 0d 49 6e 20 74 68 69 73 |rbatim}.|.In this|
|00002bb0| 20 63 61 73 65 2c 20 69 | 74 20 72 65 74 75 72 6e | case, i|t return|
|00002bc0| 73 20 61 6e 20 69 6e 74 | 65 67 65 72 20 6f 62 6a |s an int|eger obj|
|00002bd0| 65 63 74 20 28 79 65 73 | 2c 20 65 76 65 6e 20 69 |ect (yes|, even i|
|00002be0| 6e 74 65 67 65 72 73 20 | 61 72 65 0d 6f 62 6a 65 |ntegers |are.obje|
|00002bf0| 63 74 73 20 6f 6e 20 74 | 68 65 20 68 65 61 70 20 |cts on t|he heap |
|00002c00| 69 6e 20 50 79 74 68 6f | 6e 21 29 2e 20 20 4d 6f |in Pytho|n!). Mo|
|00002c10| 72 65 20 69 6e 66 6f 20 | 6f 6e 20 5c 63 6f 64 65 |re info |on \code|
|00002c20| 7b 6d 6b 76 61 6c 75 65 | 28 29 7d 20 69 73 0d 67 |{mkvalue|()} is.g|
|00002c30| 69 76 65 6e 20 6c 61 74 | 65 72 2e 0d 0d 49 66 20 |iven lat|er...If |
|00002c40| 79 6f 75 20 68 61 64 20 | 61 20 66 75 6e 63 74 69 |you had |a functi|
|00002c50| 6f 6e 20 74 68 61 74 20 | 72 65 74 75 72 6e 65 64 |on that |returned|
|00002c60| 20 6e 6f 20 75 73 65 66 | 75 6c 20 61 72 67 75 6d | no usef|ul argum|
|00002c70| 65 6e 74 20 28 61 2e 6b | 2e 61 2e 20 61 0d 70 72 |ent (a.k|.a. a.pr|
|00002c80| 6f 63 65 64 75 72 65 29 | 2c 20 79 6f 75 20 77 6f |ocedure)|, you wo|
|00002c90| 75 6c 64 20 6e 65 65 64 | 20 74 68 69 73 20 69 64 |uld need| this id|
|00002ca0| 69 6f 6d 3a 0d 0d 5c 62 | 65 67 69 6e 7b 76 65 72 |iom:..\b|egin{ver|
|00002cb0| 62 61 74 69 6d 7d 0d 20 | 20 20 20 20 20 20 20 49 |batim}. | I|
|00002cc0| 4e 43 52 45 46 28 4e 6f | 6e 65 29 3b 0d 20 20 20 |NCREF(No|ne);. |
|00002cd0| 20 20 20 20 20 72 65 74 | 75 72 6e 20 4e 6f 6e 65 | ret|urn None|
|00002ce0| 3b 0d 5c 65 6e 64 7b 76 | 65 72 62 61 74 69 6d 7d |;.\end{v|erbatim}|
|00002cf0| 0d 0d 5c 63 6f 64 65 7b | 4e 6f 6e 65 7d 20 69 73 |..\code{|None} is|
|00002d00| 20 61 20 75 6e 69 71 75 | 65 20 50 79 74 68 6f 6e | a uniqu|e Python|
|00002d10| 20 6f 62 6a 65 63 74 20 | 72 65 70 72 65 73 65 6e | object |represen|
|00002d20| 74 69 6e 67 20 60 6e 6f | 20 76 61 6c 75 65 27 2e |ting `no| value'.|
|00002d30| 20 20 49 74 0d 64 69 66 | 66 65 72 73 20 66 72 6f | It.dif|fers fro|
|00002d40| 6d 20 5c 63 6f 64 65 7b | 4e 55 4c 4c 7d 2c 20 77 |m \code{|NULL}, w|
|00002d50| 68 69 63 68 20 6d 65 61 | 6e 73 20 60 65 72 72 6f |hich mea|ns `erro|
|00002d60| 72 27 20 69 6e 20 6d 6f | 73 74 20 63 6f 6e 74 65 |r' in mo|st conte|
|00002d70| 78 74 73 2e 0d 0d 0d 5c | 73 65 63 74 69 6f 6e 7b |xts....\|section{|
|00002d80| 54 68 65 20 6d 6f 64 75 | 6c 65 27 73 20 66 75 6e |The modu|le's fun|
|00002d90| 63 74 69 6f 6e 20 74 61 | 62 6c 65 7d 0d 0d 49 20 |ction ta|ble}..I |
|00002da0| 70 72 6f 6d 69 73 65 64 | 20 74 6f 20 73 68 6f 77 |promised| to show|
|00002db0| 20 68 6f 77 20 49 20 6d | 61 64 65 20 74 68 65 20 | how I m|ade the |
|00002dc0| 66 75 6e 63 74 69 6f 6e | 20 5c 63 6f 64 65 7b 70 |function| \code{p|
|00002dd0| 6f 73 69 78 5f 73 79 73 | 74 65 6d 28 29 7d 0d 63 |osix_sys|tem()}.c|
|00002de0| 61 6c 6c 61 62 6c 65 20 | 66 72 6f 6d 20 50 79 74 |allable |from Pyt|
|00002df0| 68 6f 6e 20 70 72 6f 67 | 72 61 6d 73 2e 20 20 54 |hon prog|rams. T|
|00002e00| 68 69 73 20 69 73 20 73 | 68 6f 77 6e 20 6c 61 74 |his is s|hown lat|
|00002e10| 65 72 20 69 6e 0d 5c 66 | 69 6c 65 7b 4d 6f 64 75 |er in.\f|ile{Modu|
|00002e20| 6c 65 73 2f 70 6f 73 69 | 78 6d 6f 64 75 6c 65 2e |les/posi|xmodule.|
|00002e30| 63 7d 3a 0d 0d 5c 62 65 | 67 69 6e 7b 76 65 72 62 |c}:..\be|gin{verb|
|00002e40| 61 74 69 6d 7d 0d 20 20 | 20 20 73 74 61 74 69 63 |atim}. | static|
|00002e50| 20 73 74 72 75 63 74 20 | 6d 65 74 68 6f 64 6c 69 | struct |methodli|
|00002e60| 73 74 20 70 6f 73 69 78 | 5f 6d 65 74 68 6f 64 73 |st posix|_methods|
|00002e70| 5b 5d 20 3d 20 7b 0d 20 | 20 20 20 20 20 20 20 2e |[] = {. | .|
|00002e80| 2e 2e 0d 20 20 20 20 20 | 20 20 20 7b 22 73 79 73 |... | {"sys|
|00002e90| 74 65 6d 22 2c 20 20 70 | 6f 73 69 78 5f 73 79 73 |tem", p|osix_sys|
|00002ea0| 74 65 6d 7d 2c 0d 20 20 | 20 20 20 20 20 20 2e 2e |tem},. | ..|
|00002eb0| 2e 0d 20 20 20 20 20 20 | 20 20 7b 4e 55 4c 4c 2c |.. | {NULL,|
|00002ec0| 20 20 20 20 20 20 4e 55 | 4c 4c 7d 20 20 20 20 20 | NU|LL} |
|00002ed0| 20 20 20 2f 2a 20 53 65 | 6e 74 69 6e 65 6c 20 2a | /* Se|ntinel *|
|00002ee0| 2f 0d 20 20 20 20 7d 3b | 0d 0d 20 20 20 20 76 6f |/. };|.. vo|
|00002ef0| 69 64 0d 20 20 20 20 69 | 6e 69 74 70 6f 73 69 78 |id. i|nitposix|
|00002f00| 28 29 0d 20 20 20 20 7b | 0d 20 20 20 20 20 20 20 |(). {|. |
|00002f10| 20 28 76 6f 69 64 29 20 | 69 6e 69 74 6d 6f 64 75 | (void) |initmodu|
|00002f20| 6c 65 28 22 70 6f 73 69 | 78 22 2c 20 70 6f 73 69 |le("posi|x", posi|
|00002f30| 78 5f 6d 65 74 68 6f 64 | 73 29 3b 0d 20 20 20 20 |x_method|s);. |
|00002f40| 7d 0d 5c 65 6e 64 7b 76 | 65 72 62 61 74 69 6d 7d |}.\end{v|erbatim}|
|00002f50| 0d 0d 28 54 68 65 20 61 | 63 74 75 61 6c 20 5c 63 |..(The a|ctual \c|
|00002f60| 6f 64 65 7b 69 6e 69 74 | 70 6f 73 69 78 28 29 7d |ode{init|posix()}|
|00002f70| 20 69 73 20 73 6f 6d 65 | 77 68 61 74 20 6d 6f 72 | is some|what mor|
|00002f80| 65 20 63 6f 6d 70 6c 69 | 63 61 74 65 64 2c 20 62 |e compli|cated, b|
|00002f90| 75 74 20 6d 61 6e 79 0d | 65 78 74 65 6e 73 69 6f |ut many.|extensio|
|00002fa0| 6e 20 6d 6f 64 75 6c 65 | 73 20 63 61 6e 20 62 65 |n module|s can be|
|00002fb0| 20 61 73 20 73 69 6d 70 | 6c 65 20 61 73 20 73 68 | as simp|le as sh|
|00002fc0| 6f 77 6e 20 68 65 72 65 | 2e 29 20 20 57 68 65 6e |own here|.) When|
|00002fd0| 20 74 68 65 20 50 79 74 | 68 6f 6e 0d 70 72 6f 67 | the Pyt|hon.prog|
|00002fe0| 72 61 6d 20 66 69 72 73 | 74 20 69 6d 70 6f 72 74 |ram firs|t import|
|00002ff0| 73 20 6d 6f 64 75 6c 65 | 20 5c 63 6f 64 65 7b 70 |s module| \code{p|
|00003000| 6f 73 69 78 7d 2c 20 5c | 63 6f 64 65 7b 69 6e 69 |osix}, \|code{ini|
|00003010| 74 70 6f 73 69 78 28 29 | 7d 20 69 73 0d 63 61 6c |tposix()|} is.cal|
|00003020| 6c 65 64 2c 20 77 68 69 | 63 68 20 63 61 6c 6c 73 |led, whi|ch calls|
|00003030| 20 5c 63 6f 64 65 7b 69 | 6e 69 74 6d 6f 64 75 6c | \code{i|nitmodul|
|00003040| 65 28 29 7d 20 77 69 74 | 68 20 73 70 65 63 69 66 |e()} wit|h specif|
|00003050| 69 63 20 70 61 72 61 6d | 65 74 65 72 73 2e 0d 54 |ic param|eters..T|
|00003060| 68 69 73 20 63 72 65 61 | 74 65 73 20 61 20 60 6d |his crea|tes a `m|
|00003070| 6f 64 75 6c 65 20 6f 62 | 6a 65 63 74 27 20 28 77 |odule ob|ject' (w|
|00003080| 68 69 63 68 20 69 73 20 | 69 6e 73 65 72 74 65 64 |hich is |inserted|
|00003090| 20 69 6e 20 74 68 65 20 | 74 61 62 6c 65 0d 5c 63 | in the |table.\c|
|000030a0| 6f 64 65 7b 73 79 73 2e | 6d 6f 64 75 6c 65 73 7d |ode{sys.|modules}|
|000030b0| 20 75 6e 64 65 72 20 74 | 68 65 20 6b 65 79 20 5c | under t|he key \|
|000030c0| 63 6f 64 65 7b 27 70 6f | 73 69 78 27 7d 29 2c 20 |code{'po|six'}), |
|000030d0| 61 6e 64 20 61 64 64 73 | 0d 62 75 69 6c 74 2d 69 |and adds|.built-i|
|000030e0| 6e 2d 66 75 6e 63 74 69 | 6f 6e 20 6f 62 6a 65 63 |n-functi|on objec|
|000030f0| 74 73 20 74 6f 20 74 68 | 65 20 6e 65 77 6c 79 20 |ts to th|e newly |
|00003100| 63 72 65 61 74 65 64 20 | 6d 6f 64 75 6c 65 20 62 |created |module b|
|00003110| 61 73 65 64 20 75 70 6f | 6e 20 74 68 65 0d 74 61 |ased upo|n the.ta|
|00003120| 62 6c 65 20 28 6f 66 20 | 74 79 70 65 20 73 74 72 |ble (of |type str|
|00003130| 75 63 74 20 6d 65 74 68 | 6f 64 6c 69 73 74 29 20 |uct meth|odlist) |
|00003140| 74 68 61 74 20 77 61 73 | 20 70 61 73 73 65 64 20 |that was| passed |
|00003150| 61 73 20 69 74 73 20 73 | 65 63 6f 6e 64 0d 70 61 |as its s|econd.pa|
|00003160| 72 61 6d 65 74 65 72 2e | 20 20 54 68 65 20 66 75 |rameter.| The fu|
|00003170| 6e 63 74 69 6f 6e 20 5c | 63 6f 64 65 7b 69 6e 69 |nction \|code{ini|
|00003180| 74 6d 6f 64 75 6c 65 28 | 29 7d 20 72 65 74 75 72 |tmodule(|)} retur|
|00003190| 6e 73 20 61 20 70 6f 69 | 6e 74 65 72 20 74 6f 20 |ns a poi|nter to |
|000031a0| 74 68 65 0d 6d 6f 64 75 | 6c 65 20 6f 62 6a 65 63 |the.modu|le objec|
|000031b0| 74 20 74 68 61 74 20 69 | 74 20 63 72 65 61 74 65 |t that i|t create|
|000031c0| 73 20 28 77 68 69 63 68 | 20 69 73 20 75 6e 75 73 |s (which| is unus|
|000031d0| 65 64 20 68 65 72 65 29 | 2e 20 20 49 74 20 61 62 |ed here)|. It ab|
|000031e0| 6f 72 74 73 20 77 69 74 | 68 0d 61 20 66 61 74 61 |orts wit|h.a fata|
|000031f0| 6c 20 65 72 72 6f 72 20 | 69 66 20 74 68 65 20 6d |l error |if the m|
|00003200| 6f 64 75 6c 65 20 63 6f | 75 6c 64 20 6e 6f 74 20 |odule co|uld not |
|00003210| 62 65 20 69 6e 69 74 69 | 61 6c 69 7a 65 64 20 73 |be initi|alized s|
|00003220| 61 74 69 73 66 61 63 74 | 6f 72 69 6c 79 2c 0d 73 |atisfact|orily,.s|
|00003230| 6f 20 79 6f 75 20 64 6f | 6e 27 74 20 6e 65 65 64 |o you do|n't need|
|00003240| 20 74 6f 20 63 68 65 63 | 6b 20 66 6f 72 20 65 72 | to chec|k for er|
|00003250| 72 6f 72 73 2e 0d 0d 0d | 5c 73 65 63 74 69 6f 6e |rors....|\section|
|00003260| 7b 43 6f 6d 70 69 6c 61 | 74 69 6f 6e 20 61 6e 64 |{Compila|tion and|
|00003270| 20 6c 69 6e 6b 61 67 65 | 7d 0d 0d 54 68 65 72 65 | linkage|}..There|
|00003280| 20 61 72 65 20 74 77 6f | 20 6d 6f 72 65 20 74 68 | are two| more th|
|00003290| 69 6e 67 73 20 74 6f 20 | 64 6f 20 62 65 66 6f 72 |ings to |do befor|
|000032a0| 65 20 79 6f 75 20 63 61 | 6e 20 75 73 65 20 79 6f |e you ca|n use yo|
|000032b0| 75 72 20 6e 65 77 20 65 | 78 74 65 6e 73 69 6f 6e |ur new e|xtension|
|000032c0| 0d 6d 6f 64 75 6c 65 3a | 20 63 6f 6d 70 69 6c 69 |.module:| compili|
|000032d0| 6e 67 20 61 6e 64 20 6c | 69 6e 6b 69 6e 67 20 69 |ng and l|inking i|
|000032e0| 74 20 77 69 74 68 20 74 | 68 65 20 50 79 74 68 6f |t with t|he Pytho|
|000032f0| 6e 20 73 79 73 74 65 6d | 2e 20 20 49 66 20 79 6f |n system|. If yo|
|00003300| 75 20 75 73 65 0d 64 79 | 6e 61 6d 69 63 20 6c 6f |u use.dy|namic lo|
|00003310| 61 64 69 6e 67 2c 20 74 | 68 65 20 64 65 74 61 69 |ading, t|he detai|
|00003320| 6c 73 20 64 65 70 65 6e | 64 20 6f 6e 20 74 68 65 |ls depen|d on the|
|00003330| 20 73 74 79 6c 65 20 6f | 66 20 64 79 6e 61 6d 69 | style o|f dynami|
|00003340| 63 20 6c 6f 61 64 69 6e | 67 0d 79 6f 75 72 20 73 |c loadin|g.your s|
|00003350| 79 73 74 65 6d 20 75 73 | 65 73 3b 20 73 65 65 20 |ystem us|es; see |
|00003360| 74 68 65 20 63 68 61 70 | 74 65 72 20 6f 6e 20 44 |the chap|ter on D|
|00003370| 79 6e 61 6d 69 63 20 4c | 6f 61 64 69 6e 67 20 66 |ynamic L|oading f|
|00003380| 6f 72 20 6d 6f 72 65 20 | 69 6e 66 6f 0d 61 62 6f |or more |info.abo|
|00003390| 75 74 20 74 68 69 73 2e | 0d 0d 49 66 20 79 6f 75 |ut this.|..If you|
|000033a0| 20 63 61 6e 27 74 20 75 | 73 65 20 64 79 6e 61 6d | can't u|se dynam|
|000033b0| 69 63 20 6c 6f 61 64 69 | 6e 67 2c 20 6f 72 20 69 |ic loadi|ng, or i|
|000033c0| 66 20 79 6f 75 20 77 61 | 6e 74 20 74 6f 20 6d 61 |f you wa|nt to ma|
|000033d0| 6b 65 20 79 6f 75 72 20 | 6d 6f 64 75 6c 65 20 61 |ke your |module a|
|000033e0| 0d 70 65 72 6d 61 6e 65 | 6e 74 20 70 61 72 74 20 |.permane|nt part |
|000033f0| 6f 66 20 74 68 65 20 50 | 79 74 68 6f 6e 20 69 6e |of the P|ython in|
|00003400| 74 65 72 70 72 65 74 65 | 72 2c 20 79 6f 75 20 77 |terprete|r, you w|
|00003410| 69 6c 6c 20 68 61 76 65 | 20 74 6f 20 63 68 61 6e |ill have| to chan|
|00003420| 67 65 20 74 68 65 0d 63 | 6f 6e 66 69 67 75 72 61 |ge the.c|onfigura|
|00003430| 74 69 6f 6e 20 73 65 74 | 75 70 20 61 6e 64 20 72 |tion set|up and r|
|00003440| 65 62 75 69 6c 64 20 74 | 68 65 20 69 6e 74 65 72 |ebuild t|he inter|
|00003450| 70 72 65 74 65 72 2e 20 | 20 4c 75 63 6b 69 6c 79 |preter. | Luckily|
|00003460| 2c 20 69 6e 20 74 68 65 | 20 31 2e 30 0d 72 65 6c |, in the| 1.0.rel|
|00003470| 65 61 73 65 20 74 68 69 | 73 20 69 73 20 76 65 72 |ease thi|s is ver|
|00003480| 79 20 73 69 6d 70 6c 65 | 3a 20 6a 75 73 74 20 70 |y simple|: just p|
|00003490| 6c 61 63 65 20 79 6f 75 | 72 20 66 69 6c 65 20 28 |lace you|r file (|
|000034a0| 6e 61 6d 65 64 0d 5c 66 | 69 6c 65 7b 66 6f 6f 6d |named.\f|ile{foom|
|000034b0| 6f 64 75 6c 65 2e 63 7d | 20 66 6f 72 20 65 78 61 |odule.c}| for exa|
|000034c0| 6d 70 6c 65 29 20 69 6e | 20 74 68 65 20 5c 66 69 |mple) in| the \fi|
|000034d0| 6c 65 7b 4d 6f 64 75 6c | 65 73 7d 20 64 69 72 65 |le{Modul|es} dire|
|000034e0| 63 74 6f 72 79 2c 20 61 | 64 64 20 61 0d 6c 69 6e |ctory, a|dd a.lin|
|000034f0| 65 20 74 6f 20 74 68 65 | 20 66 69 6c 65 20 5c 66 |e to the| file \f|
|00003500| 69 6c 65 7b 4d 6f 64 75 | 6c 65 73 2f 53 65 74 75 |ile{Modu|les/Setu|
|00003510| 70 7d 20 64 65 73 63 72 | 69 62 69 6e 67 20 79 6f |p} descr|ibing yo|
|00003520| 75 72 20 66 69 6c 65 3a | 0d 0d 5c 62 65 67 69 6e |ur file:|..\begin|
|00003530| 7b 76 65 72 62 61 74 69 | 6d 7d 0d 20 20 20 20 66 |{verbati|m}. f|
|00003540| 6f 6f 20 66 6f 6f 6d 6f | 64 75 6c 65 2e 6f 0d 5c |oo foomo|dule.o.\|
|00003550| 65 6e 64 7b 76 65 72 62 | 61 74 69 6d 7d 0d 0d 61 |end{verb|atim}..a|
|00003560| 6e 64 20 72 65 62 75 69 | 6c 64 20 74 68 65 20 69 |nd rebui|ld the i|
|00003570| 6e 74 65 72 70 72 65 74 | 65 72 20 62 79 20 72 75 |nterpret|er by ru|
|00003580| 6e 6e 69 6e 67 20 5c 63 | 6f 64 65 7b 6d 61 6b 65 |nning \c|ode{make|
|00003590| 7d 20 69 6e 20 74 68 65 | 20 74 6f 70 6c 65 76 65 |} in the| topleve|
|000035a0| 6c 0d 64 69 72 65 63 74 | 6f 72 79 2e 20 20 59 6f |l.direct|ory. Yo|
|000035b0| 75 20 63 61 6e 20 61 6c | 73 6f 20 72 75 6e 20 5c |u can al|so run \|
|000035c0| 63 6f 64 65 7b 6d 61 6b | 65 7d 20 69 6e 20 74 68 |code{mak|e} in th|
|000035d0| 65 20 5c 66 69 6c 65 7b | 4d 6f 64 75 6c 65 73 7d |e \file{|Modules}|
|000035e0| 0d 73 75 62 64 69 72 65 | 63 74 6f 72 79 2c 20 62 |.subdire|ctory, b|
|000035f0| 75 74 20 74 68 65 6e 20 | 79 6f 75 20 6d 75 73 74 |ut then |you must|
|00003600| 20 66 69 72 73 74 20 72 | 65 62 75 69 6c 74 20 74 | first r|ebuilt t|
|00003610| 68 65 20 5c 66 69 6c 65 | 7b 4d 61 6b 65 66 69 6c |he \file|{Makefil|
|00003620| 65 7d 0d 74 68 65 72 65 | 20 62 79 20 72 75 6e 6e |e}.there| by runn|
|00003630| 69 6e 67 20 5c 63 6f 64 | 65 7b 6d 61 6b 65 20 4d |ing \cod|e{make M|
|00003640| 61 6b 65 66 69 6c 65 7d | 2e 20 20 28 54 68 69 73 |akefile}|. (This|
|00003650| 20 69 73 20 6e 65 63 65 | 73 73 61 72 79 20 65 61 | is nece|ssary ea|
|00003660| 63 68 20 74 69 6d 65 0d | 79 6f 75 20 63 68 61 6e |ch time.|you chan|
|00003670| 67 65 20 74 68 65 20 5c | 66 69 6c 65 7b 53 65 74 |ge the \|file{Set|
|00003680| 75 70 7d 20 66 69 6c 65 | 2e 29 0d 0d 0d 5c 73 65 |up} file|.)...\se|
|00003690| 63 74 69 6f 6e 7b 43 61 | 6c 6c 69 6e 67 20 50 79 |ction{Ca|lling Py|
|000036a0| 74 68 6f 6e 20 66 75 6e | 63 74 69 6f 6e 73 20 66 |thon fun|ctions f|
|000036b0| 72 6f 6d 20 43 7d 0d 0d | 53 6f 20 66 61 72 20 77 |rom C}..|So far w|
|000036c0| 65 20 68 61 76 65 20 63 | 6f 6e 63 65 6e 74 72 61 |e have c|oncentra|
|000036d0| 74 65 64 20 6f 6e 20 6d | 61 6b 69 6e 67 20 43 20 |ted on m|aking C |
|000036e0| 66 75 6e 63 74 69 6f 6e | 73 20 63 61 6c 6c 61 62 |function|s callab|
|000036f0| 6c 65 20 66 72 6f 6d 0d | 50 79 74 68 6f 6e 2e 20 |le from.|Python. |
|00003700| 20 54 68 65 20 72 65 76 | 65 72 73 65 20 69 73 20 | The rev|erse is |
|00003710| 61 6c 73 6f 20 75 73 65 | 66 75 6c 3a 20 63 61 6c |also use|ful: cal|
|00003720| 6c 69 6e 67 20 50 79 74 | 68 6f 6e 20 66 75 6e 63 |ling Pyt|hon func|
|00003730| 74 69 6f 6e 73 20 66 72 | 6f 6d 20 43 2e 0d 54 68 |tions fr|om C..Th|
|00003740| 69 73 20 69 73 20 65 73 | 70 65 63 69 61 6c 6c 79 |is is es|pecially|
|00003750| 20 74 68 65 20 63 61 73 | 65 20 66 6f 72 20 6c 69 | the cas|e for li|
|00003760| 62 72 61 72 69 65 73 20 | 74 68 61 74 20 73 75 70 |braries |that sup|
|00003770| 70 6f 72 74 20 73 6f 2d | 63 61 6c 6c 65 64 0d 60 |port so-|called.`|
|00003780| 63 61 6c 6c 62 61 63 6b | 27 20 66 75 6e 63 74 69 |callback|' functi|
|00003790| 6f 6e 73 2e 20 20 49 66 | 20 61 20 43 20 69 6e 74 |ons. If| a C int|
|000037a0| 65 72 66 61 63 65 20 6d | 61 6b 65 73 20 75 73 65 |erface m|akes use|
|000037b0| 20 6f 66 20 63 61 6c 6c | 62 61 63 6b 73 2c 20 74 | of call|backs, t|
|000037c0| 68 65 0d 65 71 75 69 76 | 61 6c 65 6e 74 20 50 79 |he.equiv|alent Py|
|000037d0| 74 68 6f 6e 20 6f 66 74 | 65 6e 20 6e 65 65 64 73 |thon oft|en needs|
|000037e0| 20 74 6f 20 70 72 6f 76 | 69 64 65 20 61 20 63 61 | to prov|ide a ca|
|000037f0| 6c 6c 62 61 63 6b 20 6d | 65 63 68 61 6e 69 73 6d |llback m|echanism|
|00003800| 20 74 6f 20 74 68 65 0d | 50 79 74 68 6f 6e 20 70 | to the.|Python p|
|00003810| 72 6f 67 72 61 6d 6d 65 | 72 3b 20 74 68 65 20 69 |rogramme|r; the i|
|00003820| 6d 70 6c 65 6d 65 6e 74 | 61 74 69 6f 6e 20 77 69 |mplement|ation wi|
|00003830| 6c 6c 20 72 65 71 75 69 | 72 65 20 63 61 6c 6c 69 |ll requi|re calli|
|00003840| 6e 67 20 74 68 65 20 50 | 79 74 68 6f 6e 0d 63 61 |ng the P|ython.ca|
|00003850| 6c 6c 62 61 63 6b 20 66 | 75 6e 63 74 69 6f 6e 73 |llback f|unctions|
|00003860| 20 66 72 6f 6d 20 61 20 | 43 20 63 61 6c 6c 62 61 | from a |C callba|
|00003870| 63 6b 2e 20 20 4f 74 68 | 65 72 20 75 73 65 73 20 |ck. Oth|er uses |
|00003880| 61 72 65 20 61 6c 73 6f | 20 69 6d 61 67 69 6e 61 |are also| imagina|
|00003890| 62 6c 65 2e 0d 0d 46 6f | 72 74 75 6e 61 74 65 6c |ble...Fo|rtunatel|
|000038a0| 79 2c 20 74 68 65 20 50 | 79 74 68 6f 6e 20 69 6e |y, the P|ython in|
|000038b0| 74 65 72 70 72 65 74 65 | 72 20 69 73 20 65 61 73 |terprete|r is eas|
|000038c0| 69 6c 79 20 63 61 6c 6c | 65 64 20 72 65 63 75 72 |ily call|ed recur|
|000038d0| 73 69 76 65 6c 79 2c 20 | 61 6e 64 0d 74 68 65 72 |sively, |and.ther|
|000038e0| 65 20 69 73 20 61 20 73 | 74 61 6e 64 61 72 64 20 |e is a s|tandard |
|000038f0| 69 6e 74 65 72 66 61 63 | 65 20 74 6f 20 63 61 6c |interfac|e to cal|
|00003900| 6c 20 61 20 50 79 74 68 | 6f 6e 20 66 75 6e 63 74 |l a Pyth|on funct|
|00003910| 69 6f 6e 2e 20 20 28 49 | 20 77 6f 6e 27 74 0d 64 |ion. (I| won't.d|
|00003920| 77 65 6c 6c 20 6f 6e 20 | 68 6f 77 20 74 6f 20 63 |well on |how to c|
|00003930| 61 6c 6c 20 74 68 65 20 | 50 79 74 68 6f 6e 20 70 |all the |Python p|
|00003940| 61 72 73 65 72 20 77 69 | 74 68 20 61 20 70 61 72 |arser wi|th a par|
|00003950| 74 69 63 75 6c 61 72 20 | 73 74 72 69 6e 67 20 61 |ticular |string a|
|00003960| 73 0d 69 6e 70 75 74 20 | 2d 2d 2d 20 69 66 20 79 |s.input |--- if y|
|00003970| 6f 75 27 72 65 20 69 6e | 74 65 72 65 73 74 65 64 |ou're in|terested|
|00003980| 2c 20 68 61 76 65 20 61 | 20 6c 6f 6f 6b 20 61 74 |, have a| look at|
|00003990| 20 74 68 65 20 69 6d 70 | 6c 65 6d 65 6e 74 61 74 | the imp|lementat|
|000039a0| 69 6f 6e 20 6f 66 0d 74 | 68 65 20 5c 73 61 6d 70 |ion of.t|he \samp|
|000039b0| 7b 2d 63 7d 20 63 6f 6d | 6d 61 6e 64 20 6c 69 6e |{-c} com|mand lin|
|000039c0| 65 20 6f 70 74 69 6f 6e | 20 69 6e 20 5c 66 69 6c |e option| in \fil|
|000039d0| 65 7b 50 79 74 68 6f 6e | 2f 70 79 74 68 6f 6e 6d |e{Python|/pythonm|
|000039e0| 61 69 6e 2e 63 7d 2e 29 | 0d 0d 43 61 6c 6c 69 6e |ain.c}.)|..Callin|
|000039f0| 67 20 61 20 50 79 74 68 | 6f 6e 20 66 75 6e 63 74 |g a Pyth|on funct|
|00003a00| 69 6f 6e 20 69 73 20 65 | 61 73 79 2e 20 20 46 69 |ion is e|asy. Fi|
|00003a10| 72 73 74 2c 20 74 68 65 | 20 50 79 74 68 6f 6e 20 |rst, the| Python |
|00003a20| 70 72 6f 67 72 61 6d 20 | 6d 75 73 74 0d 73 6f 6d |program |must.som|
|00003a30| 65 68 6f 77 20 70 61 73 | 73 20 79 6f 75 20 74 68 |ehow pas|s you th|
|00003a40| 65 20 50 79 74 68 6f 6e | 20 66 75 6e 63 74 69 6f |e Python| functio|
|00003a50| 6e 20 6f 62 6a 65 63 74 | 2e 20 20 59 6f 75 20 73 |n object|. You s|
|00003a60| 68 6f 75 6c 64 20 70 72 | 6f 76 69 64 65 20 61 0d |hould pr|ovide a.|
|00003a70| 66 75 6e 63 74 69 6f 6e | 20 28 6f 72 20 73 6f 6d |function| (or som|
|00003a80| 65 20 6f 74 68 65 72 20 | 69 6e 74 65 72 66 61 63 |e other |interfac|
|00003a90| 65 29 20 74 6f 20 64 6f | 20 74 68 69 73 2e 20 20 |e) to do| this. |
|00003aa0| 57 68 65 6e 20 74 68 69 | 73 20 66 75 6e 63 74 69 |When thi|s functi|
|00003ab0| 6f 6e 20 69 73 0d 63 61 | 6c 6c 65 64 2c 20 73 61 |on is.ca|lled, sa|
|00003ac0| 76 65 20 61 20 70 6f 69 | 6e 74 65 72 20 74 6f 20 |ve a poi|nter to |
|00003ad0| 74 68 65 20 50 79 74 68 | 6f 6e 20 66 75 6e 63 74 |the Pyth|on funct|
|00003ae0| 69 6f 6e 20 6f 62 6a 65 | 63 74 20 28 62 65 20 63 |ion obje|ct (be c|
|00003af0| 61 72 65 66 75 6c 20 74 | 6f 0d 5c 63 6f 64 65 7b |areful t|o.\code{|
|00003b00| 49 4e 43 52 45 46 28 29 | 7d 20 69 74 21 29 20 69 |INCREF()|} it!) i|
|00003b10| 6e 20 61 20 67 6c 6f 62 | 61 6c 20 76 61 72 69 61 |n a glob|al varia|
|00003b20| 62 6c 65 20 2d 2d 2d 20 | 6f 72 20 77 68 65 72 65 |ble --- |or where|
|00003b30| 65 76 65 72 20 79 6f 75 | 20 73 65 65 20 66 69 74 |ever you| see fit|
|00003b40| 2e 0d 46 6f 72 20 65 78 | 61 6d 70 6c 65 2c 20 74 |..For ex|ample, t|
|00003b50| 68 65 20 66 6f 6c 6c 6f | 77 69 6e 67 20 66 75 6e |he follo|wing fun|
|00003b60| 63 74 69 6f 6e 20 6d 69 | 67 68 74 20 62 65 20 70 |ction mi|ght be p|
|00003b70| 61 72 74 20 6f 66 20 61 | 20 6d 6f 64 75 6c 65 0d |art of a| module.|
|00003b80| 64 65 66 69 6e 69 74 69 | 6f 6e 3a 0d 0d 5c 62 65 |definiti|on:..\be|
|00003b90| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0d 20 20 |gin{verb|atim}. |
|00003ba0| 20 20 73 74 61 74 69 63 | 20 6f 62 6a 65 63 74 20 | static| object |
|00003bb0| 2a 6d 79 5f 63 61 6c 6c | 62 61 63 6b 20 3d 20 4e |*my_call|back = N|
|00003bc0| 55 4c 4c 3b 0d 0d 20 20 | 20 20 73 74 61 74 69 63 |ULL;.. | static|
|00003bd0| 20 6f 62 6a 65 63 74 20 | 2a 0d 20 20 20 20 6d 79 | object |*. my|
|00003be0| 5f 73 65 74 5f 63 61 6c | 6c 62 61 63 6b 28 64 75 |_set_cal|lback(du|
|00003bf0| 6d 6d 79 2c 20 61 72 67 | 29 0d 20 20 20 20 20 20 |mmy, arg|). |
|00003c00| 20 20 6f 62 6a 65 63 74 | 20 2a 64 75 6d 6d 79 2c | object| *dummy,|
|00003c10| 20 2a 61 72 67 3b 0d 20 | 20 20 20 7b 0d 20 20 20 | *arg;. | {. |
|00003c20| 20 20 20 20 20 58 44 45 | 43 52 45 46 28 6d 79 5f | XDE|CREF(my_|
|00003c30| 63 61 6c 6c 62 61 63 6b | 29 3b 20 2f 2a 20 44 69 |callback|); /* Di|
|00003c40| 73 70 6f 73 65 20 6f 66 | 20 70 72 65 76 69 6f 75 |spose of| previou|
|00003c50| 73 20 63 61 6c 6c 62 61 | 63 6b 20 2a 2f 0d 20 20 |s callba|ck */. |
|00003c60| 20 20 20 20 20 20 6d 79 | 5f 63 61 6c 6c 62 61 63 | my|_callbac|
|00003c70| 6b 20 3d 20 61 72 67 3b | 0d 20 20 20 20 20 20 20 |k = arg;|. |
|00003c80| 20 58 49 4e 43 52 45 46 | 28 6d 79 5f 63 61 6c 6c | XINCREF|(my_call|
|00003c90| 62 61 63 6b 29 3b 20 2f | 2a 20 52 65 6d 65 6d 62 |back); /|* Rememb|
|00003ca0| 65 72 20 6e 65 77 20 63 | 61 6c 6c 62 61 63 6b 20 |er new c|allback |
|00003cb0| 2a 2f 0d 20 20 20 20 20 | 20 20 20 2f 2a 20 42 6f |*/. | /* Bo|
|00003cc0| 69 6c 65 72 70 6c 61 74 | 65 20 66 6f 72 20 22 76 |ilerplat|e for "v|
|00003cd0| 6f 69 64 22 20 72 65 74 | 75 72 6e 20 2a 2f 0d 20 |oid" ret|urn */. |
|00003ce0| 20 20 20 20 20 20 20 49 | 4e 43 52 45 46 28 4e 6f | I|NCREF(No|
|00003cf0| 6e 65 29 3b 0d 20 20 20 | 20 20 20 20 20 72 65 74 |ne);. | ret|
|00003d00| 75 72 6e 20 4e 6f 6e 65 | 3b 0d 20 20 20 20 7d 0d |urn None|;. }.|
|00003d10| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 0d 0d |\end{ver|batim}..|
|00003d20| 54 68 69 73 20 70 61 72 | 74 69 63 75 6c 61 72 20 |This par|ticular |
|00003d30| 66 75 6e 63 74 69 6f 6e | 20 64 6f 65 73 6e 27 74 |function| doesn't|
|00003d40| 20 64 6f 20 61 6e 79 20 | 74 79 70 65 63 68 65 63 | do any |typechec|
|00003d50| 6b 69 6e 67 20 6f 6e 20 | 69 74 73 20 61 72 67 75 |king on |its argu|
|00003d60| 6d 65 6e 74 0d 2d 2d 2d | 20 74 68 61 74 20 77 69 |ment.---| that wi|
|00003d70| 6c 6c 20 62 65 20 64 6f | 6e 65 20 62 79 20 5c 63 |ll be do|ne by \c|
|00003d80| 6f 64 65 7b 63 61 6c 6c | 5f 6f 62 6a 65 63 74 28 |ode{call|_object(|
|00003d90| 29 7d 2c 20 77 68 69 63 | 68 20 69 73 20 61 20 62 |)}, whic|h is a b|
|00003da0| 69 74 20 6c 61 74 65 20 | 62 75 74 0d 61 74 20 6c |it late |but.at l|
|00003db0| 65 61 73 74 20 70 72 6f | 74 65 63 74 73 20 74 68 |east pro|tects th|
|00003dc0| 65 20 50 79 74 68 6f 6e | 20 69 6e 74 65 72 70 72 |e Python| interpr|
|00003dd0| 65 74 65 72 20 66 72 6f | 6d 20 73 68 6f 6f 74 69 |eter fro|m shooti|
|00003de0| 6e 67 20 69 74 73 65 6c | 66 20 69 6e 20 69 74 73 |ng itsel|f in its|
|00003df0| 0d 66 6f 6f 74 2e 20 20 | 28 54 68 65 20 70 72 6f |.foot. |(The pro|
|00003e00| 62 6c 65 6d 20 77 69 74 | 68 20 74 79 70 65 63 68 |blem wit|h typech|
|00003e10| 65 63 6b 69 6e 67 20 66 | 75 6e 63 74 69 6f 6e 73 |ecking f|unctions|
|00003e20| 20 69 73 20 74 68 61 74 | 20 74 68 65 72 65 20 61 | is that| there a|
|00003e30| 72 65 20 61 74 0d 6c 65 | 61 73 74 20 66 69 76 65 |re at.le|ast five|
|00003e40| 20 64 69 66 66 65 72 65 | 6e 74 20 50 79 74 68 6f | differe|nt Pytho|
|00003e50| 6e 20 6f 62 6a 65 63 74 | 20 74 79 70 65 73 20 74 |n object| types t|
|00003e60| 68 61 74 20 63 61 6e 20 | 62 65 20 63 61 6c 6c 65 |hat can |be calle|
|00003e70| 64 2c 20 73 6f 20 74 68 | 65 0d 74 65 73 74 20 77 |d, so th|e.test w|
|00003e80| 6f 75 6c 64 20 62 65 20 | 73 6f 6d 65 77 68 61 74 |ould be |somewhat|
|00003e90| 20 63 75 6d 62 65 72 73 | 6f 6d 65 2e 29 0d 0d 54 | cumbers|ome.)..T|
|00003ea0| 68 65 20 6d 61 63 72 6f | 73 20 5c 63 6f 64 65 7b |he macro|s \code{|
|00003eb0| 58 49 4e 43 52 45 46 28 | 29 7d 20 61 6e 64 20 5c |XINCREF(|)} and \|
|00003ec0| 63 6f 64 65 7b 58 44 45 | 43 52 45 46 28 29 7d 20 |code{XDE|CREF()} |
|00003ed0| 69 6e 63 72 65 6d 65 6e | 74 2f 64 65 63 72 65 6d |incremen|t/decrem|
|00003ee0| 65 6e 74 0d 74 68 65 20 | 72 65 66 65 72 65 6e 63 |ent.the |referenc|
|00003ef0| 65 20 63 6f 75 6e 74 20 | 6f 66 20 61 6e 20 6f 62 |e count |of an ob|
|00003f00| 6a 65 63 74 20 61 6e 64 | 20 61 72 65 20 73 61 66 |ject and| are saf|
|00003f10| 65 20 69 6e 20 74 68 65 | 20 70 72 65 73 65 6e 63 |e in the| presenc|
|00003f20| 65 20 6f 66 0d 5c 63 6f | 64 65 7b 4e 55 4c 4c 7d |e of.\co|de{NULL}|
|00003f30| 20 70 6f 69 6e 74 65 72 | 73 2e 20 20 4d 6f 72 65 | pointer|s. More|
|00003f40| 20 69 6e 66 6f 20 6f 6e | 20 74 68 65 6d 20 69 6e | info on| them in|
|00003f50| 20 74 68 65 20 73 65 63 | 74 69 6f 6e 20 6f 6e 20 | the sec|tion on |
|00003f60| 52 65 66 65 72 65 6e 63 | 65 0d 43 6f 75 6e 74 73 |Referenc|e.Counts|
|00003f70| 20 62 65 6c 6f 77 2e 0d | 0d 4c 61 74 65 72 2c 20 | below..|.Later, |
|00003f80| 77 68 65 6e 20 69 74 20 | 69 73 20 74 69 6d 65 20 |when it |is time |
|00003f90| 74 6f 20 63 61 6c 6c 20 | 74 68 65 20 66 75 6e 63 |to call |the func|
|00003fa0| 74 69 6f 6e 2c 20 79 6f | 75 20 63 61 6c 6c 20 74 |tion, yo|u call t|
|00003fb0| 68 65 20 43 20 66 75 6e | 63 74 69 6f 6e 0d 5c 63 |he C fun|ction.\c|
|00003fc0| 6f 64 65 7b 63 61 6c 6c | 5f 6f 62 6a 65 63 74 28 |ode{call|_object(|
|00003fd0| 29 7d 2e 20 20 54 68 69 | 73 20 66 75 6e 63 74 69 |)}. Thi|s functi|
|00003fe0| 6f 6e 20 68 61 73 20 74 | 77 6f 20 61 72 67 75 6d |on has t|wo argum|
|00003ff0| 65 6e 74 73 2c 20 62 6f | 74 68 20 70 6f 69 6e 74 |ents, bo|th point|
|00004000| 65 72 73 0d 74 6f 20 61 | 72 62 69 74 72 61 72 79 |ers.to a|rbitrary|
|00004010| 20 50 79 74 68 6f 6e 20 | 6f 62 6a 65 63 74 73 3a | Python |objects:|
|00004020| 20 74 68 65 20 50 79 74 | 68 6f 6e 20 66 75 6e 63 | the Pyt|hon func|
|00004030| 74 69 6f 6e 2c 20 61 6e | 64 20 74 68 65 20 61 72 |tion, an|d the ar|
|00004040| 67 75 6d 65 6e 74 0d 6c | 69 73 74 2e 20 20 54 68 |gument.l|ist. Th|
|00004050| 65 20 61 72 67 75 6d 65 | 6e 74 20 6c 69 73 74 20 |e argume|nt list |
|00004060| 6d 75 73 74 20 61 6c 77 | 61 79 73 20 62 65 20 61 |must alw|ays be a|
|00004070| 20 74 75 70 6c 65 20 6f | 62 6a 65 63 74 2c 20 77 | tuple o|bject, w|
|00004080| 68 6f 73 65 20 6c 65 6e | 67 74 68 0d 69 73 20 74 |hose len|gth.is t|
|00004090| 68 65 20 6e 75 6d 62 65 | 72 20 6f 66 20 61 72 67 |he numbe|r of arg|
|000040a0| 75 6d 65 6e 74 73 2e 20 | 20 54 6f 20 63 61 6c 6c |uments. | To call|
|000040b0| 20 74 68 65 20 50 79 74 | 68 6f 6e 20 66 75 6e 63 | the Pyt|hon func|
|000040c0| 74 69 6f 6e 20 77 69 74 | 68 20 6e 6f 0d 61 72 67 |tion wit|h no.arg|
|000040d0| 75 6d 65 6e 74 73 2c 20 | 79 6f 75 20 6d 75 73 74 |uments, |you must|
|000040e0| 20 70 61 73 73 20 61 6e | 20 65 6d 70 74 79 20 74 | pass an| empty t|
|000040f0| 75 70 6c 65 2e 20 20 46 | 6f 72 20 65 78 61 6d 70 |uple. F|or examp|
|00004100| 6c 65 3a 0d 0d 5c 62 65 | 67 69 6e 7b 76 65 72 62 |le:..\be|gin{verb|
|00004110| 61 74 69 6d 7d 0d 20 20 | 20 20 6f 62 6a 65 63 74 |atim}. | object|
|00004120| 20 2a 61 72 67 6c 69 73 | 74 3b 0d 20 20 20 20 6f | *arglis|t;. o|
|00004130| 62 6a 65 63 74 20 2a 72 | 65 73 75 6c 74 3b 0d 20 |bject *r|esult;. |
|00004140| 20 20 20 2e 2e 2e 0d 20 | 20 20 20 2f 2a 20 54 69 | .... | /* Ti|
|00004150| 6d 65 20 74 6f 20 63 61 | 6c 6c 20 74 68 65 20 63 |me to ca|ll the c|
|00004160| 61 6c 6c 62 61 63 6b 20 | 2a 2f 0d 20 20 20 20 61 |allback |*/. a|
|00004170| 72 67 6c 69 73 74 20 3d | 20 6e 65 77 74 75 70 6c |rglist =| newtupl|
|00004180| 65 6f 62 6a 65 63 74 28 | 30 29 3b 0d 20 20 20 20 |eobject(|0);. |
|00004190| 72 65 73 75 6c 74 20 3d | 20 63 61 6c 6c 5f 6f 62 |result =| call_ob|
|000041a0| 6a 65 63 74 28 6d 79 5f | 63 61 6c 6c 62 61 63 6b |ject(my_|callback|
|000041b0| 2c 20 61 72 67 6c 69 73 | 74 29 3b 0d 20 20 20 20 |, arglis|t);. |
|000041c0| 44 45 43 52 45 46 28 61 | 72 67 6c 69 73 74 29 3b |DECREF(a|rglist);|
|000041d0| 0d 5c 65 6e 64 7b 76 65 | 72 62 61 74 69 6d 7d 0d |.\end{ve|rbatim}.|
|000041e0| 0d 5c 63 6f 64 65 7b 63 | 61 6c 6c 5f 6f 62 6a 65 |.\code{c|all_obje|
|000041f0| 63 74 28 29 7d 20 72 65 | 74 75 72 6e 73 20 61 20 |ct()} re|turns a |
|00004200| 50 79 74 68 6f 6e 20 6f | 62 6a 65 63 74 20 70 6f |Python o|bject po|
|00004210| 69 6e 74 65 72 3a 20 74 | 68 69 73 20 69 73 0d 74 |inter: t|his is.t|
|00004220| 68 65 20 72 65 74 75 72 | 6e 20 76 61 6c 75 65 20 |he retur|n value |
|00004230| 6f 66 20 74 68 65 20 50 | 79 74 68 6f 6e 20 66 75 |of the P|ython fu|
|00004240| 6e 63 74 69 6f 6e 2e 20 | 20 5c 63 6f 64 65 7b 63 |nction. | \code{c|
|00004250| 61 6c 6c 5f 6f 62 6a 65 | 63 74 28 29 7d 20 69 73 |all_obje|ct()} is|
|00004260| 0d 60 72 65 66 65 72 65 | 6e 63 65 2d 63 6f 75 6e |.`refere|nce-coun|
|00004270| 74 2d 6e 65 75 74 72 61 | 6c 27 20 77 69 74 68 20 |t-neutra|l' with |
|00004280| 72 65 73 70 65 63 74 20 | 74 6f 20 69 74 73 20 61 |respect |to its a|
|00004290| 72 67 75 6d 65 6e 74 73 | 2e 20 20 49 6e 20 74 68 |rguments|. In th|
|000042a0| 65 0d 65 78 61 6d 70 6c | 65 20 61 20 6e 65 77 20 |e.exampl|e a new |
|000042b0| 74 75 70 6c 65 20 77 61 | 73 20 63 72 65 61 74 65 |tuple wa|s create|
|000042c0| 64 20 74 6f 20 73 65 72 | 76 65 20 61 73 20 74 68 |d to ser|ve as th|
|000042d0| 65 20 61 72 67 75 6d 65 | 6e 74 20 6c 69 73 74 2c |e argume|nt list,|
|000042e0| 20 77 68 69 63 68 0d 69 | 73 20 5c 63 6f 64 65 7b | which.i|s \code{|
|000042f0| 44 45 43 52 45 46 28 29 | 7d 2d 65 64 20 69 6d 6d |DECREF()|}-ed imm|
|00004300| 65 64 69 61 74 65 6c 79 | 20 61 66 74 65 72 20 74 |ediately| after t|
|00004310| 68 65 20 63 61 6c 6c 2e | 0d 0d 54 68 65 20 72 65 |he call.|..The re|
|00004320| 74 75 72 6e 20 76 61 6c | 75 65 20 6f 66 20 5c 63 |turn val|ue of \c|
|00004330| 6f 64 65 7b 63 61 6c 6c | 5f 6f 62 6a 65 63 74 28 |ode{call|_object(|
|00004340| 29 7d 20 69 73 20 60 6e | 65 77 27 3a 20 65 69 74 |)} is `n|ew': eit|
|00004350| 68 65 72 20 69 74 20 69 | 73 20 61 0d 62 72 61 6e |her it i|s a.bran|
|00004360| 64 20 6e 65 77 20 6f 62 | 6a 65 63 74 2c 20 6f 72 |d new ob|ject, or|
|00004370| 20 69 74 20 69 73 20 61 | 6e 20 65 78 69 73 74 69 | it is a|n existi|
|00004380| 6e 67 20 6f 62 6a 65 63 | 74 20 77 68 6f 73 65 20 |ng objec|t whose |
|00004390| 72 65 66 65 72 65 6e 63 | 65 20 63 6f 75 6e 74 0d |referenc|e count.|
|000043a0| 68 61 73 20 62 65 65 6e | 20 69 6e 63 72 65 6d 65 |has been| increme|
|000043b0| 6e 74 65 64 2e 20 20 53 | 6f 2c 20 75 6e 6c 65 73 |nted. S|o, unles|
|000043c0| 73 20 79 6f 75 20 77 61 | 6e 74 20 74 6f 20 73 61 |s you wa|nt to sa|
|000043d0| 76 65 20 69 74 20 69 6e | 20 61 20 67 6c 6f 62 61 |ve it in| a globa|
|000043e0| 6c 0d 76 61 72 69 61 62 | 6c 65 2c 20 79 6f 75 20 |l.variab|le, you |
|000043f0| 73 68 6f 75 6c 64 20 73 | 6f 6d 65 68 6f 77 20 5c |should s|omehow \|
|00004400| 63 6f 64 65 7b 44 45 43 | 52 45 46 28 29 7d 20 74 |code{DEC|REF()} t|
|00004410| 68 65 20 72 65 73 75 6c | 74 2c 20 65 76 65 6e 0d |he resul|t, even.|
|00004420| 28 65 73 70 65 63 69 61 | 6c 6c 79 21 29 20 69 66 |(especia|lly!) if|
|00004430| 20 79 6f 75 20 61 72 65 | 20 6e 6f 74 20 69 6e 74 | you are| not int|
|00004440| 65 72 65 73 74 65 64 20 | 69 6e 20 69 74 73 20 76 |erested |in its v|
|00004450| 61 6c 75 65 2e 0d 0d 42 | 65 66 6f 72 65 20 79 6f |alue...B|efore yo|
|00004460| 75 20 64 6f 20 74 68 69 | 73 2c 20 68 6f 77 65 76 |u do thi|s, howev|
|00004470| 65 72 2c 20 69 74 20 69 | 73 20 69 6d 70 6f 72 74 |er, it i|s import|
|00004480| 61 6e 74 20 74 6f 20 63 | 68 65 63 6b 20 74 68 61 |ant to c|heck tha|
|00004490| 74 20 74 68 65 20 72 65 | 74 75 72 6e 0d 76 61 6c |t the re|turn.val|
|000044a0| 75 65 20 69 73 6e 27 74 | 20 5c 63 6f 64 65 7b 4e |ue isn't| \code{N|
|000044b0| 55 4c 4c 7d 2e 20 20 49 | 66 20 69 74 20 69 73 2c |ULL}. I|f it is,|
|000044c0| 20 74 68 65 20 50 79 74 | 68 6f 6e 20 66 75 6e 63 | the Pyt|hon func|
|000044d0| 74 69 6f 6e 20 74 65 72 | 6d 69 6e 61 74 65 64 20 |tion ter|minated |
|000044e0| 62 79 20 72 61 69 73 69 | 6e 67 0d 61 6e 20 65 78 |by raisi|ng.an ex|
|000044f0| 63 65 70 74 69 6f 6e 2e | 20 20 49 66 20 74 68 65 |ception.| If the|
|00004500| 20 43 20 63 6f 64 65 20 | 74 68 61 74 20 63 61 6c | C code |that cal|
|00004510| 6c 65 64 20 5c 63 6f 64 | 65 7b 63 61 6c 6c 5f 6f |led \cod|e{call_o|
|00004520| 62 6a 65 63 74 28 29 7d | 20 69 73 0d 63 61 6c 6c |bject()}| is.call|
|00004530| 65 64 20 66 72 6f 6d 20 | 50 79 74 68 6f 6e 2c 20 |ed from |Python, |
|00004540| 69 74 20 73 68 6f 75 6c | 64 20 6e 6f 77 20 72 65 |it shoul|d now re|
|00004550| 74 75 72 6e 20 61 6e 20 | 65 72 72 6f 72 20 69 6e |turn an |error in|
|00004560| 64 69 63 61 74 69 6f 6e | 20 74 6f 20 69 74 73 0d |dication| to its.|
|00004570| 50 79 74 68 6f 6e 20 63 | 61 6c 6c 65 72 2c 20 73 |Python c|aller, s|
|00004580| 6f 20 74 68 65 20 69 6e | 74 65 72 70 72 65 74 65 |o the in|terprete|
|00004590| 72 20 63 61 6e 20 70 72 | 69 6e 74 20 61 20 73 74 |r can pr|int a st|
|000045a0| 61 63 6b 20 74 72 61 63 | 65 2c 20 6f 72 20 74 68 |ack trac|e, or th|
|000045b0| 65 0d 63 61 6c 6c 69 6e | 67 20 50 79 74 68 6f 6e |e.callin|g Python|
|000045c0| 20 63 6f 64 65 20 63 61 | 6e 20 68 61 6e 64 6c 65 | code ca|n handle|
|000045d0| 20 74 68 65 20 65 78 63 | 65 70 74 69 6f 6e 2e 20 | the exc|eption. |
|000045e0| 20 49 66 20 74 68 69 73 | 20 69 73 20 6e 6f 74 20 | If this| is not |
|000045f0| 70 6f 73 73 69 62 6c 65 | 0d 6f 72 20 64 65 73 69 |possible|.or desi|
|00004600| 72 61 62 6c 65 2c 20 74 | 68 65 20 65 78 63 65 70 |rable, t|he excep|
|00004610| 74 69 6f 6e 20 73 68 6f | 75 6c 64 20 62 65 20 63 |tion sho|uld be c|
|00004620| 6c 65 61 72 65 64 20 62 | 79 20 63 61 6c 6c 69 6e |leared b|y callin|
|00004630| 67 0d 5c 63 6f 64 65 7b | 65 72 72 5f 63 6c 65 61 |g.\code{|err_clea|
|00004640| 72 28 29 7d 2e 20 20 46 | 6f 72 20 65 78 61 6d 70 |r()}. F|or examp|
|00004650| 6c 65 3a 0d 0d 5c 62 65 | 67 69 6e 7b 76 65 72 62 |le:..\be|gin{verb|
|00004660| 61 74 69 6d 7d 0d 20 20 | 20 20 69 66 20 28 72 65 |atim}. | if (re|
|00004670| 73 75 6c 74 20 3d 3d 20 | 4e 55 4c 4c 29 0d 20 20 |sult == |NULL). |
|00004680| 20 20 20 20 20 20 72 65 | 74 75 72 6e 20 4e 55 4c | re|turn NUL|
|00004690| 4c 3b 20 2f 2a 20 50 61 | 73 73 20 65 72 72 6f 72 |L; /* Pa|ss error|
|000046a0| 20 62 61 63 6b 20 2a 2f | 0d 20 20 20 20 2f 2a 20 | back */|. /* |
|000046b0| 48 65 72 65 20 6d 61 79 | 62 65 20 75 73 65 20 74 |Here may|be use t|
|000046c0| 68 65 20 72 65 73 75 6c | 74 20 2a 2f 0d 20 20 20 |he resul|t */. |
|000046d0| 20 44 45 43 52 45 46 28 | 72 65 73 75 6c 74 29 3b | DECREF(|result);|
|000046e0| 20 0d 5c 65 6e 64 7b 76 | 65 72 62 61 74 69 6d 7d | .\end{v|erbatim}|
|000046f0| 0d 0d 44 65 70 65 6e 64 | 69 6e 67 20 6f 6e 20 74 |..Depend|ing on t|
|00004700| 68 65 20 64 65 73 69 72 | 65 64 20 69 6e 74 65 72 |he desir|ed inter|
|00004710| 66 61 63 65 20 74 6f 20 | 74 68 65 20 50 79 74 68 |face to |the Pyth|
|00004720| 6f 6e 20 63 61 6c 6c 62 | 61 63 6b 20 66 75 6e 63 |on callb|ack func|
|00004730| 74 69 6f 6e 2c 0d 79 6f | 75 20 6d 61 79 20 61 6c |tion,.yo|u may al|
|00004740| 73 6f 20 68 61 76 65 20 | 74 6f 20 70 72 6f 76 69 |so have |to provi|
|00004750| 64 65 20 61 6e 20 61 72 | 67 75 6d 65 6e 74 20 6c |de an ar|gument l|
|00004760| 69 73 74 20 74 6f 20 5c | 63 6f 64 65 7b 63 61 6c |ist to \|code{cal|
|00004770| 6c 5f 6f 62 6a 65 63 74 | 28 29 7d 2e 0d 49 6e 20 |l_object|()}..In |
|00004780| 73 6f 6d 65 20 63 61 73 | 65 73 20 74 68 65 20 61 |some cas|es the a|
|00004790| 72 67 75 6d 65 6e 74 20 | 6c 69 73 74 20 69 73 20 |rgument |list is |
|000047a0| 61 6c 73 6f 20 70 72 6f | 76 69 64 65 64 20 62 79 |also pro|vided by|
|000047b0| 20 74 68 65 20 50 79 74 | 68 6f 6e 0d 70 72 6f 67 | the Pyt|hon.prog|
|000047c0| 72 61 6d 2c 20 74 68 72 | 6f 75 67 68 20 74 68 65 |ram, thr|ough the|
|000047d0| 20 73 61 6d 65 20 69 6e | 74 65 72 66 61 63 65 20 | same in|terface |
|000047e0| 74 68 61 74 20 73 70 65 | 63 69 66 69 65 64 20 74 |that spe|cified t|
|000047f0| 68 65 20 63 61 6c 6c 62 | 61 63 6b 0d 66 75 6e 63 |he callb|ack.func|
|00004800| 74 69 6f 6e 2e 20 20 49 | 74 20 63 61 6e 20 74 68 |tion. I|t can th|
|00004810| 65 6e 20 62 65 20 73 61 | 76 65 64 20 61 6e 64 20 |en be sa|ved and |
|00004820| 75 73 65 64 20 69 6e 20 | 74 68 65 20 73 61 6d 65 |used in |the same|
|00004830| 20 6d 61 6e 6e 65 72 20 | 61 73 20 74 68 65 0d 66 | manner |as the.f|
|00004840| 75 6e 63 74 69 6f 6e 20 | 6f 62 6a 65 63 74 2e 20 |unction |object. |
|00004850| 20 49 6e 20 6f 74 68 65 | 72 20 63 61 73 65 73 2c | In othe|r cases,|
|00004860| 20 79 6f 75 20 6d 61 79 | 20 68 61 76 65 20 74 6f | you may| have to|
|00004870| 20 63 6f 6e 73 74 72 75 | 63 74 20 61 20 6e 65 77 | constru|ct a new|
|00004880| 0d 74 75 70 6c 65 20 74 | 6f 20 70 61 73 73 20 61 |.tuple t|o pass a|
|00004890| 73 20 74 68 65 20 61 72 | 67 75 6d 65 6e 74 20 6c |s the ar|gument l|
|000048a0| 69 73 74 2e 20 20 54 68 | 65 20 73 69 6d 70 6c 65 |ist. Th|e simple|
|000048b0| 73 74 20 77 61 79 20 74 | 6f 20 64 6f 20 74 68 69 |st way t|o do thi|
|000048c0| 73 20 69 73 20 74 6f 0d | 63 61 6c 6c 20 5c 63 6f |s is to.|call \co|
|000048d0| 64 65 7b 6d 6b 76 61 6c | 75 65 28 29 7d 2e 20 20 |de{mkval|ue()}. |
|000048e0| 46 6f 72 20 65 78 61 6d | 70 6c 65 2c 20 69 66 20 |For exam|ple, if |
|000048f0| 79 6f 75 20 77 61 6e 74 | 20 74 6f 20 70 61 73 73 |you want| to pass|
|00004900| 20 61 6e 20 69 6e 74 65 | 67 72 61 6c 0d 65 76 65 | an inte|gral.eve|
|00004910| 6e 74 20 63 6f 64 65 2c | 20 79 6f 75 20 6d 69 67 |nt code,| you mig|
|00004920| 68 74 20 75 73 65 20 74 | 68 65 20 66 6f 6c 6c 6f |ht use t|he follo|
|00004930| 77 69 6e 67 20 63 6f 64 | 65 3a 0d 0d 5c 62 65 67 |wing cod|e:..\beg|
|00004940| 69 6e 7b 76 65 72 62 61 | 74 69 6d 7d 0d 20 20 20 |in{verba|tim}. |
|00004950| 20 6f 62 6a 65 63 74 20 | 2a 61 72 67 6c 69 73 74 | object |*arglist|
|00004960| 3b 0d 20 20 20 20 2e 2e | 2e 0d 20 20 20 20 61 72 |;. ..|.. ar|
|00004970| 67 6c 69 73 74 20 3d 20 | 6d 6b 76 61 6c 75 65 28 |glist = |mkvalue(|
|00004980| 22 28 6c 29 22 2c 20 65 | 76 65 6e 74 63 6f 64 65 |"(l)", e|ventcode|
|00004990| 29 3b 0d 20 20 20 20 72 | 65 73 75 6c 74 20 3d 20 |);. r|esult = |
|000049a0| 63 61 6c 6c 5f 6f 62 6a | 65 63 74 28 6d 79 5f 63 |call_obj|ect(my_c|
|000049b0| 61 6c 6c 62 61 63 6b 2c | 20 61 72 67 6c 69 73 74 |allback,| arglist|
|000049c0| 29 3b 0d 20 20 20 20 44 | 45 43 52 45 46 28 61 72 |);. D|ECREF(ar|
|000049d0| 67 6c 69 73 74 29 3b 0d | 20 20 20 20 69 66 20 28 |glist);.| if (|
|000049e0| 72 65 73 75 6c 74 20 3d | 3d 20 4e 55 4c 4c 29 0d |result =|= NULL).|
|000049f0| 20 20 20 20 20 20 20 20 | 72 65 74 75 72 6e 20 4e | |return N|
|00004a00| 55 4c 4c 3b 20 2f 2a 20 | 50 61 73 73 20 65 72 72 |ULL; /* |Pass err|
|00004a10| 6f 72 20 62 61 63 6b 20 | 2a 2f 0d 20 20 20 20 2f |or back |*/. /|
|00004a20| 2a 20 48 65 72 65 20 6d | 61 79 62 65 20 75 73 65 |* Here m|aybe use|
|00004a30| 20 74 68 65 20 72 65 73 | 75 6c 74 20 2a 2f 0d 20 | the res|ult */. |
|00004a40| 20 20 20 44 45 43 52 45 | 46 28 72 65 73 75 6c 74 | DECRE|F(result|
|00004a50| 29 3b 0d 5c 65 6e 64 7b | 76 65 72 62 61 74 69 6d |);.\end{|verbatim|
|00004a60| 7d 0d 0d 4e 6f 74 65 20 | 74 68 65 20 70 6c 61 63 |}..Note |the plac|
|00004a70| 65 6d 65 6e 74 20 6f 66 | 20 44 45 43 52 45 46 28 |ement of| DECREF(|
|00004a80| 61 72 67 75 6d 65 6e 74 | 29 20 69 6d 6d 65 64 69 |argument|) immedi|
|00004a90| 61 74 65 6c 79 20 61 66 | 74 65 72 20 74 68 65 20 |ately af|ter the |
|00004aa0| 63 61 6c 6c 2c 0d 62 65 | 66 6f 72 65 20 74 68 65 |call,.be|fore the|
|00004ab0| 20 65 72 72 6f 72 20 63 | 68 65 63 6b 21 20 20 41 | error c|heck! A|
|00004ac0| 6c 73 6f 20 6e 6f 74 65 | 20 74 68 61 74 20 73 74 |lso note| that st|
|00004ad0| 72 69 63 74 6c 79 20 73 | 70 6f 6b 65 6e 20 74 68 |rictly s|poken th|
|00004ae0| 69 73 20 63 6f 64 65 20 | 69 73 0d 6e 6f 74 20 63 |is code |is.not c|
|00004af0| 6f 6d 70 6c 65 74 65 3a | 20 5c 63 6f 64 65 7b 6d |omplete:| \code{m|
|00004b00| 6b 76 61 6c 75 65 28 29 | 7d 20 6d 61 79 20 72 75 |kvalue()|} may ru|
|00004b10| 6e 20 6f 75 74 20 6f 66 | 20 6d 65 6d 6f 72 79 2c |n out of| memory,|
|00004b20| 20 61 6e 64 20 74 68 69 | 73 20 73 68 6f 75 6c 64 | and thi|s should|
|00004b30| 0d 62 65 20 63 68 65 63 | 6b 65 64 2e 0d 0d 0d 5c |.be chec|ked....\|
|00004b40| 73 65 63 74 69 6f 6e 7b | 46 6f 72 6d 61 74 20 73 |section{|Format s|
|00004b50| 74 72 69 6e 67 73 20 66 | 6f 72 20 7b 5c 74 74 20 |trings f|or {\tt |
|00004b60| 67 65 74 61 72 67 73 28 | 29 7d 7d 0d 0d 54 68 65 |getargs(|)}}..The|
|00004b70| 20 5c 63 6f 64 65 7b 67 | 65 74 61 72 67 73 28 29 | \code{g|etargs()|
|00004b80| 7d 20 66 75 6e 63 74 69 | 6f 6e 20 69 73 20 64 65 |} functi|on is de|
|00004b90| 63 6c 61 72 65 64 20 69 | 6e 20 5c 66 69 6c 65 7b |clared i|n \file{|
|00004ba0| 6d 6f 64 73 75 70 70 6f | 72 74 2e 68 7d 20 61 73 |modsuppo|rt.h} as|
|00004bb0| 0d 66 6f 6c 6c 6f 77 73 | 3a 0d 0d 5c 62 65 67 69 |.follows|:..\begi|
|00004bc0| 6e 7b 76 65 72 62 61 74 | 69 6d 7d 0d 20 20 20 20 |n{verbat|im}. |
|00004bd0| 69 6e 74 20 67 65 74 61 | 72 67 73 28 6f 62 6a 65 |int geta|rgs(obje|
|00004be0| 63 74 20 2a 61 72 67 2c | 20 63 68 61 72 20 2a 66 |ct *arg,| char *f|
|00004bf0| 6f 72 6d 61 74 2c 20 2e | 2e 2e 29 3b 0d 5c 65 6e |ormat, .|..);.\en|
|00004c00| 64 7b 76 65 72 62 61 74 | 69 6d 7d 0d 0d 54 68 65 |d{verbat|im}..The|
|00004c10| 20 72 65 6d 61 69 6e 69 | 6e 67 20 61 72 67 75 6d | remaini|ng argum|
|00004c20| 65 6e 74 73 20 6d 75 73 | 74 20 62 65 20 61 64 64 |ents mus|t be add|
|00004c30| 72 65 73 73 65 73 20 6f | 66 20 76 61 72 69 61 62 |resses o|f variab|
|00004c40| 6c 65 73 20 77 68 6f 73 | 65 20 74 79 70 65 20 69 |les whos|e type i|
|00004c50| 73 0d 64 65 74 65 72 6d | 69 6e 65 64 20 62 79 20 |s.determ|ined by |
|00004c60| 74 68 65 20 66 6f 72 6d | 61 74 20 73 74 72 69 6e |the form|at strin|
|00004c70| 67 2e 20 20 46 6f 72 20 | 74 68 65 20 63 6f 6e 76 |g. For |the conv|
|00004c80| 65 72 73 69 6f 6e 20 74 | 6f 20 73 75 63 63 65 65 |ersion t|o succee|
|00004c90| 64 2c 20 74 68 65 0d 5c | 76 61 72 7b 61 72 67 7d |d, the.\|var{arg}|
|00004ca0| 20 6f 62 6a 65 63 74 20 | 6d 75 73 74 20 6d 61 74 | object |must mat|
|00004cb0| 63 68 20 74 68 65 20 66 | 6f 72 6d 61 74 20 61 6e |ch the f|ormat an|
|00004cc0| 64 20 74 68 65 20 66 6f | 72 6d 61 74 20 6d 75 73 |d the fo|rmat mus|
|00004cd0| 74 20 62 65 20 65 78 68 | 61 75 73 74 65 64 2e 0d |t be exh|austed..|
|00004ce0| 4e 6f 74 65 20 74 68 61 | 74 20 77 68 69 6c 65 20 |Note tha|t while |
|00004cf0| 5c 63 6f 64 65 7b 67 65 | 74 61 72 67 73 28 29 7d |\code{ge|targs()}|
|00004d00| 20 63 68 65 63 6b 73 20 | 74 68 61 74 20 74 68 65 | checks |that the|
|00004d10| 20 50 79 74 68 6f 6e 20 | 6f 62 6a 65 63 74 20 72 | Python |object r|
|00004d20| 65 61 6c 6c 79 0d 69 73 | 20 6f 66 20 74 68 65 20 |eally.is| of the |
|00004d30| 73 70 65 63 69 66 69 65 | 64 20 74 79 70 65 2c 20 |specifie|d type, |
|00004d40| 69 74 20 63 61 6e 6e 6f | 74 20 63 68 65 63 6b 20 |it canno|t check |
|00004d50| 74 68 65 20 76 61 6c 69 | 64 69 74 79 20 6f 66 20 |the vali|dity of |
|00004d60| 74 68 65 0d 61 64 64 72 | 65 73 73 65 73 20 6f 66 |the.addr|esses of|
|00004d70| 20 43 20 76 61 72 69 61 | 62 6c 65 73 20 70 72 6f | C varia|bles pro|
|00004d80| 76 69 64 65 64 20 69 6e | 20 74 68 65 20 63 61 6c |vided in| the cal|
|00004d90| 6c 3a 20 69 66 20 79 6f | 75 20 6d 61 6b 65 20 6d |l: if yo|u make m|
|00004da0| 69 73 74 61 6b 65 73 0d | 74 68 65 72 65 2c 20 79 |istakes.|there, y|
|00004db0| 6f 75 72 20 63 6f 64 65 | 20 77 69 6c 6c 20 70 72 |our code| will pr|
|00004dc0| 6f 62 61 62 6c 79 20 64 | 75 6d 70 20 63 6f 72 65 |obably d|ump core|
|00004dd0| 2e 0d 0d 41 20 6e 6f 6e | 2d 65 6d 70 74 79 20 66 |...A non|-empty f|
|00004de0| 6f 72 6d 61 74 20 73 74 | 72 69 6e 67 20 63 6f 6e |ormat st|ring con|
|00004df0| 73 69 73 74 73 20 6f 66 | 20 61 20 73 69 6e 67 6c |sists of| a singl|
|00004e00| 65 20 60 66 6f 72 6d 61 | 74 20 75 6e 69 74 27 2e |e `forma|t unit'.|
|00004e10| 20 20 41 0d 66 6f 72 6d | 61 74 20 75 6e 69 74 20 | A.form|at unit |
|00004e20| 64 65 73 63 72 69 62 65 | 73 20 6f 6e 65 20 50 79 |describe|s one Py|
|00004e30| 74 68 6f 6e 20 6f 62 6a | 65 63 74 3b 20 69 74 20 |thon obj|ect; it |
|00004e40| 69 73 20 75 73 75 61 6c | 6c 79 20 61 20 73 69 6e |is usual|ly a sin|
|00004e50| 67 6c 65 0d 63 68 61 72 | 61 63 74 65 72 20 6f 72 |gle.char|acter or|
|00004e60| 20 61 20 70 61 72 65 6e | 74 68 65 73 69 7a 65 64 | a paren|thesized|
|00004e70| 20 73 65 71 75 65 6e 63 | 65 20 6f 66 20 66 6f 72 | sequenc|e of for|
|00004e80| 6d 61 74 20 75 6e 69 74 | 73 2e 20 20 54 68 65 20 |mat unit|s. The |
|00004e90| 74 79 70 65 20 6f 66 20 | 61 0d 66 6f 72 6d 61 74 |type of |a.format|
|00004ea0| 20 75 6e 69 74 73 20 69 | 73 20 64 65 74 65 72 6d | units i|s determ|
|00004eb0| 69 6e 65 64 20 66 72 6f | 6d 20 69 74 73 20 66 69 |ined fro|m its fi|
|00004ec0| 72 73 74 20 63 68 61 72 | 61 63 74 65 72 2c 20 74 |rst char|acter, t|
|00004ed0| 68 65 20 60 66 6f 72 6d | 61 74 0d 6c 65 74 74 65 |he `form|at.lette|
|00004ee0| 72 27 3a 0d 0d 5c 62 65 | 67 69 6e 7b 64 65 73 63 |r':..\be|gin{desc|
|00004ef0| 72 69 70 74 69 6f 6e 7d | 0d 0d 5c 69 74 65 6d 5b |ription}|..\item[|
|00004f00| 5c 73 61 6d 70 7b 73 7d | 20 28 73 74 72 69 6e 67 |\samp{s}| (string|
|00004f10| 29 5d 0d 54 68 65 20 50 | 79 74 68 6f 6e 20 6f 62 |)].The P|ython ob|
|00004f20| 6a 65 63 74 20 6d 75 73 | 74 20 62 65 20 61 20 73 |ject mus|t be a s|
|00004f30| 74 72 69 6e 67 20 6f 62 | 6a 65 63 74 2e 20 20 54 |tring ob|ject. T|
|00004f40| 68 65 20 43 20 61 72 67 | 75 6d 65 6e 74 20 6d 75 |he C arg|ument mu|
|00004f50| 73 74 20 62 65 20 61 0d | 5c 63 6f 64 65 7b 28 63 |st be a.|\code{(c|
|00004f60| 68 61 72 2a 2a 29 7d 20 | 28 69 2e 65 2e 20 74 68 |har**)} |(i.e. th|
|00004f70| 65 20 61 64 64 72 65 73 | 73 20 6f 66 20 61 20 63 |e addres|s of a c|
|00004f80| 68 61 72 61 63 74 65 72 | 20 70 6f 69 6e 74 65 72 |haracter| pointer|
|00004f90| 29 2c 20 61 6e 64 20 61 | 20 70 6f 69 6e 74 65 72 |), and a| pointer|
|00004fa0| 0d 74 6f 20 74 68 65 20 | 43 20 73 74 72 69 6e 67 |.to the |C string|
|00004fb0| 20 63 6f 6e 74 61 69 6e | 65 64 20 69 6e 20 74 68 | contain|ed in th|
|00004fc0| 65 20 50 79 74 68 6f 6e | 20 6f 62 6a 65 63 74 20 |e Python| object |
|00004fd0| 69 73 20 73 74 6f 72 65 | 64 20 69 6e 74 6f 20 69 |is store|d into i|
|00004fe0| 74 2e 20 20 59 6f 75 0d | 6d 75 73 74 20 6e 6f 74 |t. You.|must not|
|00004ff0| 20 70 72 6f 76 69 64 65 | 20 73 74 6f 72 61 67 65 | provide| storage|
|00005000| 20 74 6f 20 73 74 6f 72 | 65 20 74 68 65 20 73 74 | to stor|e the st|
|00005010| 72 69 6e 67 3b 20 61 20 | 70 6f 69 6e 74 65 72 20 |ring; a |pointer |
|00005020| 74 6f 20 61 6e 20 65 78 | 69 73 74 69 6e 67 0d 73 |to an ex|isting.s|
|00005030| 74 72 69 6e 67 20 69 73 | 20 73 74 6f 72 65 64 20 |tring is| stored |
|00005040| 69 6e 74 6f 20 74 68 65 | 20 63 68 61 72 61 63 74 |into the| charact|
|00005050| 65 72 20 70 6f 69 6e 74 | 65 72 20 76 61 72 69 61 |er point|er varia|
|00005060| 62 6c 65 20 77 68 6f 73 | 65 20 61 64 64 72 65 73 |ble whos|e addres|
|00005070| 73 20 79 6f 75 0d 70 61 | 73 73 2e 20 20 49 66 20 |s you.pa|ss. If |
|00005080| 74 68 65 20 6e 65 78 74 | 20 63 68 61 72 61 63 74 |the next| charact|
|00005090| 65 72 20 69 6e 20 74 68 | 65 20 66 6f 72 6d 61 74 |er in th|e format|
|000050a0| 20 73 74 72 69 6e 67 20 | 69 73 20 5c 73 61 6d 70 | string |is \samp|
|000050b0| 7b 5c 23 7d 2c 0d 61 6e | 6f 74 68 65 72 20 43 20 |{\#},.an|other C |
|000050c0| 61 72 67 75 6d 65 6e 74 | 20 6f 66 20 74 79 70 65 |argument| of type|
|000050d0| 20 5c 63 6f 64 65 7b 28 | 69 6e 74 2a 29 7d 20 6d | \code{(|int*)} m|
|000050e0| 75 73 74 20 62 65 20 70 | 72 65 73 65 6e 74 2c 20 |ust be p|resent, |
|000050f0| 61 6e 64 20 74 68 65 0d | 6c 65 6e 67 74 68 20 6f |and the.|length o|
|00005100| 66 20 74 68 65 20 50 79 | 74 68 6f 6e 20 73 74 72 |f the Py|thon str|
|00005110| 69 6e 67 20 28 6e 6f 74 | 20 63 6f 75 6e 74 69 6e |ing (not| countin|
|00005120| 67 20 74 68 65 20 74 72 | 61 69 6c 69 6e 67 20 7a |g the tr|ailing z|
|00005130| 65 72 6f 20 62 79 74 65 | 29 20 69 73 0d 73 74 6f |ero byte|) is.sto|
|00005140| 72 65 64 20 69 6e 74 6f | 20 69 74 2e 0d 0d 5c 69 |red into| it...\i|
|00005150| 74 65 6d 5b 5c 73 61 6d | 70 7b 7a 7d 20 28 73 74 |tem[\sam|p{z} (st|
|00005160| 72 69 6e 67 20 6f 72 20 | 7a 65 72 6f 2c 20 69 2e |ring or |zero, i.|
|00005170| 65 2e 20 5c 63 6f 64 65 | 7b 4e 55 4c 4c 7d 29 5d |e. \code|{NULL})]|
|00005180| 0d 4c 69 6b 65 20 5c 73 | 61 6d 70 7b 73 7d 2c 20 |.Like \s|amp{s}, |
|00005190| 62 75 74 20 74 68 65 20 | 6f 62 6a 65 63 74 20 6d |but the |object m|
|000051a0| 61 79 20 61 6c 73 6f 20 | 62 65 20 4e 6f 6e 65 2e |ay also |be None.|
|000051b0| 20 20 49 6e 20 74 68 69 | 73 20 63 61 73 65 20 74 | In thi|s case t|
|000051c0| 68 65 0d 73 74 72 69 6e | 67 20 70 6f 69 6e 74 65 |he.strin|g pointe|
|000051d0| 72 20 69 73 20 73 65 74 | 20 74 6f 20 5c 63 6f 64 |r is set| to \cod|
|000051e0| 65 7b 4e 55 4c 4c 7d 20 | 61 6e 64 20 69 66 20 61 |e{NULL} |and if a|
|000051f0| 20 5c 73 61 6d 70 7b 5c | 23 7d 20 69 73 20 70 72 | \samp{\|#} is pr|
|00005200| 65 73 65 6e 74 20 74 68 | 65 0d 73 69 7a 65 20 69 |esent th|e.size i|
|00005210| 73 20 73 65 74 20 74 6f | 20 30 2e 0d 0d 5c 69 74 |s set to| 0...\it|
|00005220| 65 6d 5b 5c 73 61 6d 70 | 7b 62 7d 20 28 62 79 74 |em[\samp|{b} (byt|
|00005230| 65 2c 20 69 2e 65 2e 20 | 63 68 61 72 20 69 6e 74 |e, i.e. |char int|
|00005240| 65 72 70 72 65 74 65 64 | 20 61 73 20 74 69 6e 79 |erpreted| as tiny|
|00005250| 20 69 6e 74 29 5d 0d 54 | 68 65 20 6f 62 6a 65 63 | int)].T|he objec|
|00005260| 74 20 6d 75 73 74 20 62 | 65 20 61 20 50 79 74 68 |t must b|e a Pyth|
|00005270| 6f 6e 20 69 6e 74 65 67 | 65 72 2e 20 20 54 68 65 |on integ|er. The|
|00005280| 20 43 20 61 72 67 75 6d | 65 6e 74 20 6d 75 73 74 | C argum|ent must|
|00005290| 20 62 65 20 61 0d 5c 63 | 6f 64 65 7b 28 63 68 61 | be a.\c|ode{(cha|
|000052a0| 72 2a 29 7d 2e 0d 0d 5c | 69 74 65 6d 5b 5c 73 61 |r*)}...\|item[\sa|
|000052b0| 6d 70 7b 68 7d 20 28 68 | 61 6c 66 2c 20 69 2e 65 |mp{h} (h|alf, i.e|
|000052c0| 2e 20 73 68 6f 72 74 29 | 5d 0d 54 68 65 20 6f 62 |. short)|].The ob|
|000052d0| 6a 65 63 74 20 6d 75 73 | 74 20 62 65 20 61 20 50 |ject mus|t be a P|
|000052e0| 79 74 68 6f 6e 20 69 6e | 74 65 67 65 72 2e 20 20 |ython in|teger. |
|000052f0| 54 68 65 20 43 20 61 72 | 67 75 6d 65 6e 74 20 6d |The C ar|gument m|
|00005300| 75 73 74 20 62 65 20 61 | 0d 5c 63 6f 64 65 7b 28 |ust be a|.\code{(|
|00005310| 73 68 6f 72 74 2a 29 7d | 2e 0d 0d 5c 69 74 65 6d |short*)}|...\item|
|00005320| 5b 5c 73 61 6d 70 7b 69 | 7d 20 28 69 6e 74 29 5d |[\samp{i|} (int)]|
|00005330| 0d 54 68 65 20 6f 62 6a | 65 63 74 20 6d 75 73 74 |.The obj|ect must|
|00005340| 20 62 65 20 61 20 50 79 | 74 68 6f 6e 20 69 6e 74 | be a Py|thon int|
|00005350| 65 67 65 72 2e 20 20 54 | 68 65 20 43 20 61 72 67 |eger. T|he C arg|
|00005360| 75 6d 65 6e 74 20 6d 75 | 73 74 20 62 65 20 61 6e |ument mu|st be an|
|00005370| 0d 5c 63 6f 64 65 7b 28 | 69 6e 74 2a 29 7d 2e 0d |.\code{(|int*)}..|
|00005380| 0d 5c 69 74 65 6d 5b 5c | 73 61 6d 70 7b 6c 7d 20 |.\item[\|samp{l} |
|00005390| 28 6c 6f 6e 67 29 5d 0d | 54 68 65 20 6f 62 6a 65 |(long)].|The obje|
|000053a0| 63 74 20 6d 75 73 74 20 | 62 65 20 61 20 28 70 6c |ct must |be a (pl|
|000053b0| 61 69 6e 21 29 20 50 79 | 74 68 6f 6e 20 69 6e 74 |ain!) Py|thon int|
|000053c0| 65 67 65 72 2e 20 20 54 | 68 65 20 43 20 61 72 67 |eger. T|he C arg|
|000053d0| 75 6d 65 6e 74 20 6d 75 | 73 74 20 62 65 0d 61 20 |ument mu|st be.a |
|000053e0| 5c 63 6f 64 65 7b 28 6c | 6f 6e 67 2a 29 7d 2e 0d |\code{(l|ong*)}..|
|000053f0| 0d 5c 69 74 65 6d 5b 5c | 73 61 6d 70 7b 63 7d 20 |.\item[\|samp{c} |
|00005400| 28 63 68 61 72 29 5d 0d | 54 68 65 20 50 79 74 68 |(char)].|The Pyth|
|00005410| 6f 6e 20 6f 62 6a 65 63 | 74 20 6d 75 73 74 20 62 |on objec|t must b|
|00005420| 65 20 61 20 73 74 72 69 | 6e 67 20 6f 66 20 6c 65 |e a stri|ng of le|
|00005430| 6e 67 74 68 20 31 2e 20 | 20 54 68 65 20 43 20 61 |ngth 1. | The C a|
|00005440| 72 67 75 6d 65 6e 74 20 | 6d 75 73 74 0d 62 65 20 |rgument |must.be |
|00005450| 61 20 5c 63 6f 64 65 7b | 28 63 68 61 72 2a 29 7d |a \code{|(char*)}|
|00005460| 2e 20 20 28 44 6f 6e 27 | 74 20 70 61 73 73 20 61 |. (Don'|t pass a|
|00005470| 6e 20 5c 63 6f 64 65 7b | 28 69 6e 74 2a 29 7d 21 |n \code{|(int*)}!|
|00005480| 29 0d 0d 5c 69 74 65 6d | 5b 5c 73 61 6d 70 7b 66 |)..\item|[\samp{f|
|00005490| 7d 20 28 66 6c 6f 61 74 | 29 5d 0d 54 68 65 20 6f |} (float|)].The o|
|000054a0| 62 6a 65 63 74 20 6d 75 | 73 74 20 62 65 20 61 20 |bject mu|st be a |
|000054b0| 50 79 74 68 6f 6e 20 69 | 6e 74 20 6f 72 20 66 6c |Python i|nt or fl|
|000054c0| 6f 61 74 2e 20 20 54 68 | 65 20 43 20 61 72 67 75 |oat. Th|e C argu|
|000054d0| 6d 65 6e 74 20 6d 75 73 | 74 20 62 65 20 61 0d 5c |ment mus|t be a.\|
|000054e0| 63 6f 64 65 7b 28 66 6c | 6f 61 74 2a 29 7d 2e 0d |code{(fl|oat*)}..|
|000054f0| 0d 5c 69 74 65 6d 5b 5c | 73 61 6d 70 7b 64 7d 20 |.\item[\|samp{d} |
|00005500| 28 64 6f 75 62 6c 65 29 | 5d 0d 54 68 65 20 6f 62 |(double)|].The ob|
|00005510| 6a 65 63 74 20 6d 75 73 | 74 20 62 65 20 61 20 50 |ject mus|t be a P|
|00005520| 79 74 68 6f 6e 20 69 6e | 74 20 6f 72 20 66 6c 6f |ython in|t or flo|
|00005530| 61 74 2e 20 20 54 68 65 | 20 43 20 61 72 67 75 6d |at. The| C argum|
|00005540| 65 6e 74 20 6d 75 73 74 | 20 62 65 20 61 0d 5c 63 |ent must| be a.\c|
|00005550| 6f 64 65 7b 28 64 6f 75 | 62 6c 65 2a 29 7d 2e 0d |ode{(dou|ble*)}..|
|00005560| 0d 5c 69 74 65 6d 5b 5c | 73 61 6d 70 7b 53 7d 20 |.\item[\|samp{S} |
|00005570| 28 73 74 72 69 6e 67 20 | 6f 62 6a 65 63 74 29 5d |(string |object)]|
|00005580| 0d 54 68 65 20 6f 62 6a | 65 63 74 20 6d 75 73 74 |.The obj|ect must|
|00005590| 20 62 65 20 61 20 50 79 | 74 68 6f 6e 20 73 74 72 | be a Py|thon str|
|000055a0| 69 6e 67 2e 20 20 54 68 | 65 20 43 20 61 72 67 75 |ing. Th|e C argu|
|000055b0| 6d 65 6e 74 20 6d 75 73 | 74 20 62 65 20 61 6e 0d |ment mus|t be an.|
|000055c0| 5c 63 6f 64 65 7b 28 6f | 62 6a 65 63 74 2a 2a 29 |\code{(o|bject**)|
|000055d0| 7d 20 28 69 2e 65 2e 20 | 74 68 65 20 61 64 64 72 |} (i.e. |the addr|
|000055e0| 65 73 73 20 6f 66 20 61 | 6e 20 6f 62 6a 65 63 74 |ess of a|n object|
|000055f0| 20 70 6f 69 6e 74 65 72 | 29 2e 20 20 54 68 65 20 | pointer|). The |
|00005600| 43 0d 70 72 6f 67 72 61 | 6d 20 74 68 75 73 20 67 |C.progra|m thus g|
|00005610| 65 74 73 20 62 61 63 6b | 20 74 68 65 20 61 63 74 |ets back| the act|
|00005620| 75 61 6c 20 73 74 72 69 | 6e 67 20 6f 62 6a 65 63 |ual stri|ng objec|
|00005630| 74 20 74 68 61 74 20 77 | 61 73 20 70 61 73 73 65 |t that w|as passe|
|00005640| 64 2c 20 6e 6f 74 0d 6a | 75 73 74 20 61 20 70 6f |d, not.j|ust a po|
|00005650| 69 6e 74 65 72 20 74 6f | 20 69 74 73 20 61 72 72 |inter to| its arr|
|00005660| 61 79 20 6f 66 20 63 68 | 61 72 61 63 74 65 72 73 |ay of ch|aracters|
|00005670| 20 61 6e 64 20 69 74 73 | 20 73 69 7a 65 20 61 73 | and its| size as|
|00005680| 20 66 6f 72 20 66 6f 72 | 6d 61 74 0d 63 68 61 72 | for for|mat.char|
|00005690| 61 63 74 65 72 20 5c 73 | 61 6d 70 7b 73 7d 2e 20 |acter \s|amp{s}. |
|000056a0| 20 54 68 65 20 72 65 66 | 65 72 65 6e 63 65 20 63 | The ref|erence c|
|000056b0| 6f 75 6e 74 20 6f 66 20 | 74 68 65 20 6f 62 6a 65 |ount of |the obje|
|000056c0| 63 74 20 68 61 73 20 6e | 6f 74 20 62 65 65 6e 0d |ct has n|ot been.|
|000056d0| 69 6e 63 72 65 61 73 65 | 64 2e 0d 0d 5c 69 74 65 |increase|d...\ite|
|000056e0| 6d 5b 5c 73 61 6d 70 7b | 4f 7d 20 28 6f 62 6a 65 |m[\samp{|O} (obje|
|000056f0| 63 74 29 5d 0d 54 68 65 | 20 6f 62 6a 65 63 74 20 |ct)].The| object |
|00005700| 63 61 6e 20 62 65 20 61 | 6e 79 20 50 79 74 68 6f |can be a|ny Pytho|
|00005710| 6e 20 6f 62 6a 65 63 74 | 2c 20 69 6e 63 6c 75 64 |n object|, includ|
|00005720| 69 6e 67 20 4e 6f 6e 65 | 2c 20 62 75 74 20 6e 6f |ing None|, but no|
|00005730| 74 0d 5c 63 6f 64 65 7b | 4e 55 4c 4c 7d 2e 20 20 |t.\code{|NULL}. |
|00005740| 54 68 65 20 43 20 61 72 | 67 75 6d 65 6e 74 20 6d |The C ar|gument m|
|00005750| 75 73 74 20 62 65 20 61 | 6e 20 5c 63 6f 64 65 7b |ust be a|n \code{|
|00005760| 28 6f 62 6a 65 63 74 2a | 2a 29 7d 2e 20 20 54 68 |(object*|*)}. Th|
|00005770| 69 73 20 63 61 6e 20 62 | 65 0d 75 73 65 64 20 69 |is can b|e.used i|
|00005780| 66 20 61 6e 20 61 72 67 | 75 6d 65 6e 74 20 6c 69 |f an arg|ument li|
|00005790| 73 74 20 6d 75 73 74 20 | 63 6f 6e 74 61 69 6e 20 |st must |contain |
|000057a0| 6f 62 6a 65 63 74 73 20 | 6f 66 20 61 20 74 79 70 |objects |of a typ|
|000057b0| 65 20 66 6f 72 20 77 68 | 69 63 68 20 6e 6f 0d 66 |e for wh|ich no.f|
|000057c0| 6f 72 6d 61 74 20 6c 65 | 74 74 65 72 20 65 78 69 |ormat le|tter exi|
|000057d0| 73 74 3a 20 74 68 65 20 | 63 61 6c 6c 65 72 20 6d |st: the |caller m|
|000057e0| 75 73 74 20 74 68 65 6e | 20 63 68 65 63 6b 20 74 |ust then| check t|
|000057f0| 68 61 74 20 69 74 20 68 | 61 73 20 74 68 65 20 72 |hat it h|as the r|
|00005800| 69 67 68 74 0d 74 79 70 | 65 2e 20 20 54 68 65 20 |ight.typ|e. The |
|00005810| 72 65 66 65 72 65 6e 63 | 65 20 63 6f 75 6e 74 20 |referenc|e count |
|00005820| 6f 66 20 74 68 65 20 6f | 62 6a 65 63 74 20 68 61 |of the o|bject ha|
|00005830| 73 20 6e 6f 74 20 62 65 | 65 6e 20 69 6e 63 72 65 |s not be|en incre|
|00005840| 61 73 65 64 2e 0d 0d 5c | 69 74 65 6d 5b 5c 73 61 |ased...\|item[\sa|
|00005850| 6d 70 7b 28 7d 20 28 74 | 75 70 6c 65 29 5d 0d 54 |mp{(} (t|uple)].T|
|00005860| 68 65 20 6f 62 6a 65 63 | 74 20 6d 75 73 74 20 62 |he objec|t must b|
|00005870| 65 20 61 20 50 79 74 68 | 6f 6e 20 74 75 70 6c 65 |e a Pyth|on tuple|
|00005880| 2e 20 20 46 6f 6c 6c 6f | 77 69 6e 67 20 74 68 65 |. Follo|wing the|
|00005890| 20 5c 73 61 6d 70 7b 28 | 7d 20 63 68 61 72 61 63 | \samp{(|} charac|
|000058a0| 74 65 72 0d 69 6e 20 74 | 68 65 20 66 6f 72 6d 61 |ter.in t|he forma|
|000058b0| 74 20 73 74 72 69 6e 67 | 20 6d 75 73 74 20 63 6f |t string| must co|
|000058c0| 6d 65 20 61 20 6e 75 6d | 62 65 72 20 6f 66 20 66 |me a num|ber of f|
|000058d0| 6f 72 6d 61 74 20 75 6e | 69 74 73 20 64 65 73 63 |ormat un|its desc|
|000058e0| 72 69 62 69 6e 67 20 74 | 68 65 0d 65 6c 65 6d 65 |ribing t|he.eleme|
|000058f0| 6e 74 73 20 6f 66 20 74 | 68 65 20 74 75 70 6c 65 |nts of t|he tuple|
|00005900| 2c 20 66 6f 6c 6c 6f 77 | 65 64 20 62 79 20 61 20 |, follow|ed by a |
|00005910| 5c 73 61 6d 70 7b 29 7d | 20 63 68 61 72 61 63 74 |\samp{)}| charact|
|00005920| 65 72 2e 20 20 54 75 70 | 6c 65 0d 66 6f 72 6d 61 |er. Tup|le.forma|
|00005930| 74 20 75 6e 69 74 73 20 | 6d 61 79 20 62 65 20 6e |t units |may be n|
|00005940| 65 73 74 65 64 2e 20 20 | 28 54 68 65 72 65 20 61 |ested. |(There a|
|00005950| 72 65 20 6e 6f 20 65 78 | 63 65 70 74 69 6f 6e 73 |re no ex|ceptions|
|00005960| 20 66 6f 72 20 65 6d 70 | 74 79 20 61 6e 64 0d 73 | for emp|ty and.s|
|00005970| 69 6e 67 6c 65 74 6f 6e | 20 74 75 70 6c 65 73 3b |ingleton| tuples;|
|00005980| 20 5c 73 61 6d 70 7b 28 | 29 7d 20 73 70 65 63 69 | \samp{(|)} speci|
|00005990| 66 69 65 73 20 61 6e 20 | 65 6d 70 74 79 20 74 75 |fies an |empty tu|
|000059a0| 70 6c 65 20 61 6e 64 20 | 5c 73 61 6d 70 7b 28 69 |ple and |\samp{(i|
|000059b0| 29 7d 20 61 0d 73 69 6e | 67 6c 65 74 6f 6e 20 6f |)} a.sin|gleton o|
|000059c0| 66 20 6f 6e 65 20 69 6e | 74 65 67 65 72 2e 20 20 |f one in|teger. |
|000059d0| 4e 6f 72 6d 61 6c 6c 79 | 20 79 6f 75 20 64 6f 6e |Normally| you don|
|000059e0| 27 74 20 77 61 6e 74 20 | 74 6f 20 75 73 65 20 74 |'t want |to use t|
|000059f0| 68 65 20 6c 61 74 74 65 | 72 2c 0d 73 69 6e 63 65 |he latte|r,.since|
|00005a00| 20 69 74 20 69 73 20 68 | 61 72 64 20 66 6f 72 20 | it is h|ard for |
|00005a10| 74 68 65 20 50 79 74 68 | 6f 6e 20 75 73 65 72 20 |the Pyth|on user |
|00005a20| 74 6f 20 73 70 65 63 69 | 66 79 2e 0d 0d 5c 65 6e |to speci|fy...\en|
|00005a30| 64 7b 64 65 73 63 72 69 | 70 74 69 6f 6e 7d 0d 0d |d{descri|ption}..|
|00005a40| 4d 6f 72 65 20 66 6f 72 | 6d 61 74 20 63 68 61 72 |More for|mat char|
|00005a50| 61 63 74 65 72 73 20 77 | 69 6c 6c 20 70 72 6f 62 |acters w|ill prob|
|00005a60| 61 62 6c 79 20 62 65 20 | 61 64 64 65 64 20 61 73 |ably be |added as|
|00005a70| 20 74 68 65 20 6e 65 65 | 64 20 61 72 69 73 65 73 | the nee|d arises|
|00005a80| 2e 20 20 49 74 0d 73 68 | 6f 75 6c 64 20 28 62 75 |. It.sh|ould (bu|
|00005a90| 74 20 63 75 72 72 65 6e | 74 6c 79 20 69 73 6e 27 |t curren|tly isn'|
|00005aa0| 74 29 20 62 65 20 61 6c | 6c 6f 77 65 64 20 74 6f |t) be al|lowed to|
|00005ab0| 20 75 73 65 20 50 79 74 | 68 6f 6e 20 6c 6f 6e 67 | use Pyt|hon long|
|00005ac0| 20 69 6e 74 65 67 65 72 | 73 0d 77 68 65 72 65 65 | integer|s.wheree|
|00005ad0| 76 65 72 20 69 6e 74 65 | 67 65 72 73 20 61 72 65 |ver inte|gers are|
|00005ae0| 20 65 78 70 65 63 74 65 | 64 2c 20 61 6e 64 20 70 | expecte|d, and p|
|00005af0| 65 72 66 6f 72 6d 20 61 | 20 72 61 6e 67 65 20 63 |erform a| range c|
|00005b00| 68 65 63 6b 2e 20 20 28 | 41 20 72 61 6e 67 65 0d |heck. (|A range.|
|00005b10| 63 68 65 63 6b 20 69 73 | 20 69 6e 20 66 61 63 74 |check is| in fact|
|00005b20| 20 61 6c 77 61 79 73 20 | 6e 65 63 65 73 73 61 72 | always |necessar|
|00005b30| 79 20 66 6f 72 20 74 68 | 65 20 5c 73 61 6d 70 7b |y for th|e \samp{|
|00005b40| 62 7d 2c 20 5c 73 61 6d | 70 7b 68 7d 20 61 6e 64 |b}, \sam|p{h} and|
|00005b50| 0d 5c 73 61 6d 70 7b 69 | 7d 20 66 6f 72 6d 61 74 |.\samp{i|} format|
|00005b60| 20 6c 65 74 74 65 72 73 | 2c 20 62 75 74 20 74 68 | letters|, but th|
|00005b70| 69 73 20 69 73 20 63 75 | 72 72 65 6e 74 6c 79 20 |is is cu|rrently |
|00005b80| 6e 6f 74 20 69 6d 70 6c | 65 6d 65 6e 74 65 64 2e |not impl|emented.|
|00005b90| 29 0d 0d 53 6f 6d 65 20 | 65 78 61 6d 70 6c 65 20 |)..Some |example |
|00005ba0| 63 61 6c 6c 73 3a 0d 0d | 5c 62 65 67 69 6e 7b 76 |calls:..|\begin{v|
|00005bb0| 65 72 62 61 74 69 6d 7d | 0d 20 20 20 20 69 6e 74 |erbatim}|. int|
|00005bc0| 20 6f 6b 3b 0d 20 20 20 | 20 69 6e 74 20 69 2c 20 | ok;. | int i, |
|00005bd0| 6a 3b 0d 20 20 20 20 6c | 6f 6e 67 20 6b 2c 20 6c |j;. l|ong k, l|
|00005be0| 3b 0d 20 20 20 20 63 68 | 61 72 20 2a 73 3b 0d 20 |;. ch|ar *s;. |
|00005bf0| 20 20 20 69 6e 74 20 73 | 69 7a 65 3b 0d 0d 20 20 | int s|ize;.. |
|00005c00| 20 20 6f 6b 20 3d 20 67 | 65 74 61 72 67 73 28 61 | ok = g|etargs(a|
|00005c10| 72 67 73 2c 20 22 22 29 | 3b 20 2f 2a 20 4e 6f 20 |rgs, "")|; /* No |
|00005c20| 61 72 67 75 6d 65 6e 74 | 73 20 2a 2f 0d 20 20 20 |argument|s */. |
|00005c30| 20 20 20 20 20 2f 2a 20 | 50 79 74 68 6f 6e 20 63 | /* |Python c|
|00005c40| 61 6c 6c 3a 20 66 28 29 | 20 2a 2f 0d 20 20 20 20 |all: f()| */. |
|00005c50| 0d 20 20 20 20 6f 6b 20 | 3d 20 67 65 74 61 72 67 |. ok |= getarg|
|00005c60| 73 28 61 72 67 73 2c 20 | 22 73 22 2c 20 26 73 29 |s(args, |"s", &s)|
|00005c70| 3b 20 2f 2a 20 41 20 73 | 74 72 69 6e 67 20 2a 2f |; /* A s|tring */|
|00005c80| 0d 20 20 20 20 20 20 20 | 20 2f 2a 20 50 6f 73 73 |. | /* Poss|
|00005c90| 69 62 6c 65 20 50 79 74 | 68 6f 6e 20 63 61 6c 6c |ible Pyt|hon call|
|00005ca0| 3a 20 66 28 27 77 68 6f | 6f 70 73 21 27 29 20 2a |: f('who|ops!') *|
|00005cb0| 2f 0d 0d 20 20 20 20 6f | 6b 20 3d 20 67 65 74 61 |/.. o|k = geta|
|00005cc0| 72 67 73 28 61 72 67 73 | 2c 20 22 28 6c 6c 73 29 |rgs(args|, "(lls)|
|00005cd0| 22 2c 20 26 6b 2c 20 26 | 6c 2c 20 26 73 29 3b 20 |", &k, &|l, &s); |
|00005ce0| 2f 2a 20 54 77 6f 20 6c | 6f 6e 67 73 20 61 6e 64 |/* Two l|ongs and|
|00005cf0| 20 61 20 73 74 72 69 6e | 67 20 2a 2f 0d 20 20 20 | a strin|g */. |
|00005d00| 20 20 20 20 20 2f 2a 20 | 50 6f 73 73 69 62 6c 65 | /* |Possible|
|00005d10| 20 50 79 74 68 6f 6e 20 | 63 61 6c 6c 3a 20 66 28 | Python |call: f(|
|00005d20| 31 2c 20 32 2c 20 27 74 | 68 72 65 65 27 29 20 2a |1, 2, 't|hree') *|
|00005d30| 2f 0d 20 20 20 20 0d 20 | 20 20 20 6f 6b 20 3d 20 |/. . | ok = |
|00005d40| 67 65 74 61 72 67 73 28 | 61 72 67 73 2c 20 22 28 |getargs(|args, "(|
|00005d50| 28 69 69 29 73 23 29 22 | 2c 20 26 69 2c 20 26 6a |(ii)s#)"|, &i, &j|
|00005d60| 2c 20 26 73 2c 20 26 73 | 69 7a 65 29 3b 0d 20 20 |, &s, &s|ize);. |
|00005d70| 20 20 20 20 20 20 2f 2a | 20 41 20 70 61 69 72 20 | /*| A pair |
|00005d80| 6f 66 20 69 6e 74 73 20 | 61 6e 64 20 61 20 73 74 |of ints |and a st|
|00005d90| 72 69 6e 67 2c 20 77 68 | 6f 73 65 20 73 69 7a 65 |ring, wh|ose size|
|00005da0| 20 69 73 20 61 6c 73 6f | 20 72 65 74 75 72 6e 65 | is also| returne|
|00005db0| 64 20 2a 2f 0d 20 20 20 | 20 20 20 20 20 2f 2a 20 |d */. | /* |
|00005dc0| 50 6f 73 73 69 62 6c 65 | 20 50 79 74 68 6f 6e 20 |Possible| Python |
|00005dd0| 63 61 6c 6c 3a 20 66 28 | 31 2c 20 32 2c 20 27 74 |call: f(|1, 2, 't|
|00005de0| 68 72 65 65 27 29 20 2a | 2f 0d 0d 20 20 20 20 7b |hree') *|/.. {|
|00005df0| 0d 20 20 20 20 20 20 20 | 20 69 6e 74 20 6c 65 66 |. | int lef|
|00005e00| 74 2c 20 74 6f 70 2c 20 | 72 69 67 68 74 2c 20 62 |t, top, |right, b|
|00005e10| 6f 74 74 6f 6d 2c 20 68 | 2c 20 76 3b 0d 20 20 20 |ottom, h|, v;. |
|00005e20| 20 20 20 20 20 6f 6b 20 | 3d 20 67 65 74 61 72 67 | ok |= getarg|
|00005e30| 73 28 61 72 67 73 2c 20 | 22 28 28 28 69 69 29 28 |s(args, |"(((ii)(|
|00005e40| 69 69 29 29 28 69 69 29 | 29 22 2c 0d 20 20 20 20 |ii))(ii)|)",. |
|00005e50| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 26 6c 65 | | &le|
|00005e60| 66 74 2c 20 26 74 6f 70 | 2c 20 26 72 69 67 68 74 |ft, &top|, &right|
|00005e70| 2c 20 26 62 6f 74 74 6f | 6d 2c 20 26 68 2c 20 26 |, &botto|m, &h, &|
|00005e80| 76 29 3b 0d 20 20 20 20 | 20 20 20 20 20 20 20 20 |v);. | |
|00005e90| 20 20 20 20 20 2f 2a 20 | 41 20 72 65 63 74 61 6e | /* |A rectan|
|00005ea0| 67 6c 65 20 61 6e 64 20 | 61 20 70 6f 69 6e 74 20 |gle and |a point |
|00005eb0| 2a 2f 0d 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |*/. | |
|00005ec0| 20 20 20 20 2f 2a 20 50 | 6f 73 73 69 62 6c 65 20 | /* P|ossible |
|00005ed0| 50 79 74 68 6f 6e 20 63 | 61 6c 6c 3a 0d 20 20 20 |Python c|all:. |
|00005ee0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00005ef0| 20 66 28 20 28 28 30 2c | 20 30 29 2c 20 28 34 30 | f( ((0,| 0), (40|
|00005f00| 30 2c 20 33 30 30 29 29 | 2c 20 28 31 30 2c 20 31 |0, 300))|, (10, 1|
|00005f10| 30 29 29 20 2a 2f 0d 20 | 20 20 20 7d 0d 5c 65 6e |0)) */. | }.\en|
|00005f20| 64 7b 76 65 72 62 61 74 | 69 6d 7d 0d 0d 4e 6f 74 |d{verbat|im}..Not|
|00005f30| 65 20 74 68 61 74 20 74 | 68 65 20 60 74 6f 70 20 |e that t|he `top |
|00005f40| 6c 65 76 65 6c 27 20 6f | 66 20 61 20 6e 6f 6e 2d |level' o|f a non-|
|00005f50| 65 6d 70 74 79 20 66 6f | 72 6d 61 74 20 73 74 72 |empty fo|rmat str|
|00005f60| 69 6e 67 20 6d 75 73 74 | 20 63 6f 6e 73 69 73 74 |ing must| consist|
|00005f70| 20 6f 66 0d 61 20 73 69 | 6e 67 6c 65 20 75 6e 69 | of.a si|ngle uni|
|00005f80| 74 3b 20 73 74 72 69 6e | 67 73 20 6c 69 6b 65 20 |t; strin|gs like |
|00005f90| 5c 73 61 6d 70 7b 69 73 | 7d 20 61 6e 64 20 5c 73 |\samp{is|} and \s|
|00005fa0| 61 6d 70 7b 28 69 69 29 | 73 5c 23 7d 20 61 72 65 |amp{(ii)|s\#} are|
|00005fb0| 20 6e 6f 74 20 76 61 6c | 69 64 0d 66 6f 72 6d 61 | not val|id.forma|
|00005fc0| 74 20 73 74 72 69 6e 67 | 73 2e 20 20 28 42 75 74 |t string|s. (But|
|00005fd0| 20 5c 73 61 6d 70 7b 73 | 5c 23 7d 20 69 73 2e 29 | \samp{s|\#} is.)|
|00005fe0| 20 20 49 66 20 79 6f 75 | 20 68 61 76 65 20 6d 75 | If you| have mu|
|00005ff0| 6c 74 69 70 6c 65 20 61 | 72 67 75 6d 65 6e 74 73 |ltiple a|rguments|
|00006000| 2c 0d 74 68 65 20 66 6f | 72 6d 61 74 20 6d 75 73 |,.the fo|rmat mus|
|00006010| 74 20 74 68 65 72 65 66 | 6f 72 65 20 61 6c 77 61 |t theref|ore alwa|
|00006020| 79 73 20 62 65 20 65 6e | 63 6c 6f 73 65 64 20 69 |ys be en|closed i|
|00006030| 6e 20 70 61 72 65 6e 74 | 68 65 73 65 73 2c 20 61 |n parent|heses, a|
|00006040| 73 20 69 6e 20 74 68 65 | 0d 65 78 61 6d 70 6c 65 |s in the|.example|
|00006050| 73 20 5c 73 61 6d 70 7b | 28 28 69 69 29 73 5c 23 |s \samp{|((ii)s\#|
|00006060| 29 7d 20 61 6e 64 20 5c | 73 61 6d 70 7b 28 28 28 |)} and \|samp{(((|
|00006070| 69 69 29 28 69 69 29 29 | 28 69 69 29 7d 2e 20 20 |ii)(ii))|(ii)}. |
|00006080| 28 54 68 65 20 63 75 72 | 72 65 6e 74 0d 69 6d 70 |(The cur|rent.imp|
|00006090| 6c 65 6d 65 6e 74 61 74 | 69 6f 6e 20 64 6f 65 73 |lementat|ion does|
|000060a0| 20 6e 6f 74 20 63 6f 6d | 70 6c 61 69 6e 20 77 68 | not com|plain wh|
|000060b0| 65 6e 20 6d 6f 72 65 20 | 74 68 61 6e 20 6f 6e 65 |en more |than one|
|000060c0| 20 75 6e 70 61 72 65 6e | 74 68 65 73 69 7a 65 64 | unparen|thesized|
|000060d0| 0d 66 6f 72 6d 61 74 20 | 75 6e 69 74 20 69 73 20 |.format |unit is |
|000060e0| 67 69 76 65 6e 2e 20 20 | 53 6f 72 72 79 2e 29 0d |given. |Sorry.).|
|000060f0| 0d 54 68 65 20 5c 63 6f | 64 65 7b 67 65 74 61 72 |.The \co|de{getar|
|00006100| 67 73 28 29 7d 20 66 75 | 6e 63 74 69 6f 6e 20 64 |gs()} fu|nction d|
|00006110| 6f 65 73 20 6e 6f 74 20 | 73 75 70 70 6f 72 74 20 |oes not |support |
|00006120| 76 61 72 69 61 62 6c 65 | 2d 6c 65 6e 67 74 68 0d |variable|-length.|
|00006130| 61 72 67 75 6d 65 6e 74 | 20 6c 69 73 74 73 2e 20 |argument| lists. |
|00006140| 20 49 6e 20 73 69 6d 70 | 6c 65 20 63 61 73 65 73 | In simp|le cases|
|00006150| 20 79 6f 75 20 63 61 6e | 20 66 61 6b 65 20 74 68 | you can| fake th|
|00006160| 65 73 65 20 62 79 20 74 | 72 79 69 6e 67 20 73 65 |ese by t|rying se|
|00006170| 76 65 72 61 6c 0d 63 61 | 6c 6c 73 20 74 6f 0d 5c |veral.ca|lls to.\|
|00006180| 63 6f 64 65 7b 67 65 74 | 61 72 67 73 28 29 7d 20 |code{get|args()} |
|00006190| 75 6e 74 69 6c 20 6f 6e | 65 20 73 75 63 63 65 65 |until on|e succee|
|000061a0| 64 73 2c 20 62 75 74 20 | 79 6f 75 20 6d 75 73 74 |ds, but |you must|
|000061b0| 20 74 61 6b 65 20 63 61 | 72 65 20 74 6f 20 63 61 | take ca|re to ca|
|000061c0| 6c 6c 0d 5c 63 6f 64 65 | 7b 65 72 72 5f 63 6c 65 |ll.\code|{err_cle|
|000061d0| 61 72 28 29 7d 20 62 65 | 66 6f 72 65 20 65 61 63 |ar()} be|fore eac|
|000061e0| 68 20 72 65 74 72 79 2e | 20 20 46 6f 72 20 65 78 |h retry.| For ex|
|000061f0| 61 6d 70 6c 65 3a 0d 0d | 5c 62 65 67 69 6e 7b 76 |ample:..|\begin{v|
|00006200| 65 72 62 61 74 69 6d 7d | 0d 20 20 20 20 73 74 61 |erbatim}|. sta|
|00006210| 74 69 63 20 6f 62 6a 65 | 63 74 20 2a 6d 79 5f 6d |tic obje|ct *my_m|
|00006220| 65 74 68 6f 64 28 73 65 | 6c 66 2c 20 61 72 67 73 |ethod(se|lf, args|
|00006230| 29 20 6f 62 6a 65 63 74 | 20 2a 73 65 6c 66 2c 20 |) object| *self, |
|00006240| 2a 61 72 67 73 3b 20 7b | 0d 20 20 20 20 20 20 20 |*args; {|. |
|00006250| 20 69 6e 74 20 69 2c 20 | 6a 2c 20 6b 3b 0d 0d 20 | int i, |j, k;.. |
|00006260| 20 20 20 20 20 20 20 69 | 66 20 28 67 65 74 61 72 | i|f (getar|
|00006270| 67 73 28 61 72 67 73 2c | 20 22 28 69 69 29 22 2c |gs(args,| "(ii)",|
|00006280| 20 26 69 2c 20 26 6a 29 | 29 20 7b 0d 20 20 20 20 | &i, &j)|) {. |
|00006290| 20 20 20 20 20 20 20 20 | 6b 20 3d 20 30 3b 20 2f | |k = 0; /|
|000062a0| 2a 20 55 73 65 20 64 65 | 66 61 75 6c 74 20 74 68 |* Use de|fault th|
|000062b0| 69 72 64 20 61 72 67 75 | 6d 65 6e 74 20 2a 2f 0d |ird argu|ment */.|
|000062c0| 20 20 20 20 20 20 20 20 | 7d 0d 20 20 20 20 20 20 | |}. |
|000062d0| 20 20 65 6c 73 65 20 7b | 0d 20 20 20 20 20 20 20 | else {|. |
|000062e0| 20 20 20 20 20 65 72 72 | 5f 63 6c 65 61 72 28 29 | err|_clear()|
|000062f0| 3b 0d 20 20 20 20 20 20 | 20 20 20 20 20 20 69 66 |;. | if|
|00006300| 20 28 21 67 65 74 61 72 | 67 73 28 61 72 67 73 2c | (!getar|gs(args,|
|00006310| 20 22 28 69 69 69 29 22 | 2c 20 26 69 2c 20 26 6a | "(iii)"|, &i, &j|
|00006320| 2c 20 26 6b 29 29 0d 20 | 20 20 20 20 20 20 20 20 |, &k)). | |
|00006330| 20 20 20 20 20 20 20 72 | 65 74 75 72 6e 20 4e 55 | r|eturn NU|
|00006340| 4c 4c 3b 0d 20 20 20 20 | 20 20 20 20 7d 0d 20 20 |LL;. | }. |
|00006350| 20 20 20 20 20 20 2f 2a | 20 2e 2e 2e 20 75 73 65 | /*| ... use|
|00006360| 20 69 2c 20 6a 20 61 6e | 64 20 6b 20 68 65 72 65 | i, j an|d k here|
|00006370| 20 2e 2e 2e 20 2a 2f 0d | 20 20 20 20 20 20 20 20 | ... */.| |
|00006380| 49 4e 43 52 45 46 28 4e | 6f 6e 65 29 3b 0d 20 20 |INCREF(N|one);. |
|00006390| 20 20 20 20 20 20 72 65 | 74 75 72 6e 20 4e 6f 6e | re|turn Non|
|000063a0| 65 3b 0d 20 20 20 20 7d | 0d 5c 65 6e 64 7b 76 65 |e;. }|.\end{ve|
|000063b0| 72 62 61 74 69 6d 7d 0d | 0d 28 49 74 20 69 73 20 |rbatim}.|.(It is |
|000063c0| 70 6f 73 73 69 62 6c 65 | 20 74 6f 20 74 68 69 6e |possible| to thin|
|000063d0| 6b 20 6f 66 20 61 6e 20 | 65 78 74 65 6e 73 69 6f |k of an |extensio|
|000063e0| 6e 20 74 6f 20 74 68 65 | 20 64 65 66 69 6e 69 74 |n to the| definit|
|000063f0| 69 6f 6e 20 6f 66 20 66 | 6f 72 6d 61 74 0d 73 74 |ion of f|ormat.st|
+--------+-------------------------+-------------------------+--------+--------+
Only 25.0 KB of data is shown above.